util.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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(struct ieee80211_local *local, 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 (local->hw.conf.channel->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. size_t frame_len,
  144. struct ieee80211_rate *rate)
  145. {
  146. struct ieee80211_local *local = hw_to_local(hw);
  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(local, 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[local->hw.conf.channel->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(local, 10, rate->bitrate,
  186. erp, short_preamble);
  187. /* Data frame duration */
  188. dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
  189. erp, short_preamble);
  190. /* ACK duration */
  191. dur += ieee80211_frame_duration(local, 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[local->hw.conf.channel->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(local, 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(local, 10, rate->bitrate,
  224. erp, short_preamble);
  225. }
  226. return cpu_to_le16(dur);
  227. }
  228. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  229. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  230. enum queue_stop_reason reason)
  231. {
  232. struct ieee80211_local *local = hw_to_local(hw);
  233. struct ieee80211_sub_if_data *sdata;
  234. trace_wake_queue(local, queue, reason);
  235. if (WARN_ON(queue >= hw->queues))
  236. return;
  237. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  238. if (local->queue_stop_reasons[queue] != 0)
  239. /* someone still has this queue stopped */
  240. return;
  241. if (skb_queue_empty(&local->pending[queue])) {
  242. rcu_read_lock();
  243. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  244. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  245. continue;
  246. netif_wake_subqueue(sdata->dev, queue);
  247. }
  248. rcu_read_unlock();
  249. } else
  250. tasklet_schedule(&local->tx_pending_tasklet);
  251. }
  252. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  253. enum queue_stop_reason reason)
  254. {
  255. struct ieee80211_local *local = hw_to_local(hw);
  256. unsigned long flags;
  257. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  258. __ieee80211_wake_queue(hw, queue, reason);
  259. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  260. }
  261. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  262. {
  263. ieee80211_wake_queue_by_reason(hw, queue,
  264. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  265. }
  266. EXPORT_SYMBOL(ieee80211_wake_queue);
  267. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  268. enum queue_stop_reason reason)
  269. {
  270. struct ieee80211_local *local = hw_to_local(hw);
  271. struct ieee80211_sub_if_data *sdata;
  272. trace_stop_queue(local, queue, reason);
  273. if (WARN_ON(queue >= hw->queues))
  274. return;
  275. __set_bit(reason, &local->queue_stop_reasons[queue]);
  276. rcu_read_lock();
  277. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  278. netif_stop_subqueue(sdata->dev, queue);
  279. rcu_read_unlock();
  280. }
  281. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  282. enum queue_stop_reason reason)
  283. {
  284. struct ieee80211_local *local = hw_to_local(hw);
  285. unsigned long flags;
  286. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  287. __ieee80211_stop_queue(hw, queue, reason);
  288. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  289. }
  290. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  291. {
  292. ieee80211_stop_queue_by_reason(hw, queue,
  293. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  294. }
  295. EXPORT_SYMBOL(ieee80211_stop_queue);
  296. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  297. struct sk_buff *skb)
  298. {
  299. struct ieee80211_hw *hw = &local->hw;
  300. unsigned long flags;
  301. int queue = skb_get_queue_mapping(skb);
  302. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  303. if (WARN_ON(!info->control.vif)) {
  304. kfree_skb(skb);
  305. return;
  306. }
  307. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  308. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  309. __skb_queue_tail(&local->pending[queue], skb);
  310. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  311. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  312. }
  313. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  314. struct sk_buff_head *skbs,
  315. void (*fn)(void *data), void *data)
  316. {
  317. struct ieee80211_hw *hw = &local->hw;
  318. struct sk_buff *skb;
  319. unsigned long flags;
  320. int queue, i;
  321. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  322. for (i = 0; i < hw->queues; i++)
  323. __ieee80211_stop_queue(hw, i,
  324. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  325. while ((skb = skb_dequeue(skbs))) {
  326. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  327. if (WARN_ON(!info->control.vif)) {
  328. kfree_skb(skb);
  329. continue;
  330. }
  331. queue = skb_get_queue_mapping(skb);
  332. __skb_queue_tail(&local->pending[queue], skb);
  333. }
  334. if (fn)
  335. fn(data);
  336. for (i = 0; i < hw->queues; i++)
  337. __ieee80211_wake_queue(hw, i,
  338. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  339. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  340. }
  341. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  342. struct sk_buff_head *skbs)
  343. {
  344. ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
  345. }
  346. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  347. enum queue_stop_reason reason)
  348. {
  349. struct ieee80211_local *local = hw_to_local(hw);
  350. unsigned long flags;
  351. int i;
  352. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  353. for (i = 0; i < hw->queues; i++)
  354. __ieee80211_stop_queue(hw, i, reason);
  355. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  356. }
  357. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  358. {
  359. ieee80211_stop_queues_by_reason(hw,
  360. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  361. }
  362. EXPORT_SYMBOL(ieee80211_stop_queues);
  363. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  364. {
  365. struct ieee80211_local *local = hw_to_local(hw);
  366. unsigned long flags;
  367. int ret;
  368. if (WARN_ON(queue >= hw->queues))
  369. return true;
  370. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  371. ret = !!local->queue_stop_reasons[queue];
  372. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL(ieee80211_queue_stopped);
  376. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  377. enum queue_stop_reason reason)
  378. {
  379. struct ieee80211_local *local = hw_to_local(hw);
  380. unsigned long flags;
  381. int i;
  382. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  383. for (i = 0; i < hw->queues; i++)
  384. __ieee80211_wake_queue(hw, i, reason);
  385. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  386. }
  387. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  388. {
  389. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  390. }
  391. EXPORT_SYMBOL(ieee80211_wake_queues);
  392. void ieee80211_iterate_active_interfaces(
  393. struct ieee80211_hw *hw,
  394. void (*iterator)(void *data, u8 *mac,
  395. struct ieee80211_vif *vif),
  396. void *data)
  397. {
  398. struct ieee80211_local *local = hw_to_local(hw);
  399. struct ieee80211_sub_if_data *sdata;
  400. mutex_lock(&local->iflist_mtx);
  401. list_for_each_entry(sdata, &local->interfaces, list) {
  402. switch (sdata->vif.type) {
  403. case NL80211_IFTYPE_MONITOR:
  404. case NL80211_IFTYPE_AP_VLAN:
  405. continue;
  406. default:
  407. break;
  408. }
  409. if (ieee80211_sdata_running(sdata))
  410. iterator(data, sdata->vif.addr,
  411. &sdata->vif);
  412. }
  413. mutex_unlock(&local->iflist_mtx);
  414. }
  415. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  416. void ieee80211_iterate_active_interfaces_atomic(
  417. struct ieee80211_hw *hw,
  418. void (*iterator)(void *data, u8 *mac,
  419. struct ieee80211_vif *vif),
  420. void *data)
  421. {
  422. struct ieee80211_local *local = hw_to_local(hw);
  423. struct ieee80211_sub_if_data *sdata;
  424. rcu_read_lock();
  425. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  426. switch (sdata->vif.type) {
  427. case NL80211_IFTYPE_MONITOR:
  428. case NL80211_IFTYPE_AP_VLAN:
  429. continue;
  430. default:
  431. break;
  432. }
  433. if (ieee80211_sdata_running(sdata))
  434. iterator(data, sdata->vif.addr,
  435. &sdata->vif);
  436. }
  437. rcu_read_unlock();
  438. }
  439. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  440. /*
  441. * Nothing should have been stuffed into the workqueue during
  442. * the suspend->resume cycle. If this WARN is seen then there
  443. * is a bug with either the driver suspend or something in
  444. * mac80211 stuffing into the workqueue which we haven't yet
  445. * cleared during mac80211's suspend cycle.
  446. */
  447. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  448. {
  449. if (WARN(local->suspended && !local->resuming,
  450. "queueing ieee80211 work while going to suspend\n"))
  451. return false;
  452. return true;
  453. }
  454. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  455. {
  456. struct ieee80211_local *local = hw_to_local(hw);
  457. if (!ieee80211_can_queue_work(local))
  458. return;
  459. queue_work(local->workqueue, work);
  460. }
  461. EXPORT_SYMBOL(ieee80211_queue_work);
  462. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  463. struct delayed_work *dwork,
  464. unsigned long delay)
  465. {
  466. struct ieee80211_local *local = hw_to_local(hw);
  467. if (!ieee80211_can_queue_work(local))
  468. return;
  469. queue_delayed_work(local->workqueue, dwork, delay);
  470. }
  471. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  472. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  473. struct ieee802_11_elems *elems,
  474. u64 filter, u32 crc)
  475. {
  476. size_t left = len;
  477. u8 *pos = start;
  478. bool calc_crc = filter != 0;
  479. DECLARE_BITMAP(seen_elems, 256);
  480. bitmap_zero(seen_elems, 256);
  481. memset(elems, 0, sizeof(*elems));
  482. elems->ie_start = start;
  483. elems->total_len = len;
  484. while (left >= 2) {
  485. u8 id, elen;
  486. bool elem_parse_failed;
  487. id = *pos++;
  488. elen = *pos++;
  489. left -= 2;
  490. if (elen > left) {
  491. elems->parse_error = true;
  492. break;
  493. }
  494. if (id != WLAN_EID_VENDOR_SPECIFIC &&
  495. id != WLAN_EID_QUIET &&
  496. test_bit(id, seen_elems)) {
  497. elems->parse_error = true;
  498. left -= elen;
  499. pos += elen;
  500. continue;
  501. }
  502. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  503. crc = crc32_be(crc, pos - 2, elen + 2);
  504. elem_parse_failed = false;
  505. switch (id) {
  506. case WLAN_EID_SSID:
  507. elems->ssid = pos;
  508. elems->ssid_len = elen;
  509. break;
  510. case WLAN_EID_SUPP_RATES:
  511. elems->supp_rates = pos;
  512. elems->supp_rates_len = elen;
  513. break;
  514. case WLAN_EID_FH_PARAMS:
  515. elems->fh_params = pos;
  516. elems->fh_params_len = elen;
  517. break;
  518. case WLAN_EID_DS_PARAMS:
  519. elems->ds_params = pos;
  520. elems->ds_params_len = elen;
  521. break;
  522. case WLAN_EID_CF_PARAMS:
  523. elems->cf_params = pos;
  524. elems->cf_params_len = elen;
  525. break;
  526. case WLAN_EID_TIM:
  527. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  528. elems->tim = (void *)pos;
  529. elems->tim_len = elen;
  530. } else
  531. elem_parse_failed = true;
  532. break;
  533. case WLAN_EID_IBSS_PARAMS:
  534. elems->ibss_params = pos;
  535. elems->ibss_params_len = elen;
  536. break;
  537. case WLAN_EID_CHALLENGE:
  538. elems->challenge = pos;
  539. elems->challenge_len = elen;
  540. break;
  541. case WLAN_EID_VENDOR_SPECIFIC:
  542. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  543. pos[2] == 0xf2) {
  544. /* Microsoft OUI (00:50:F2) */
  545. if (calc_crc)
  546. crc = crc32_be(crc, pos - 2, elen + 2);
  547. if (pos[3] == 1) {
  548. /* OUI Type 1 - WPA IE */
  549. elems->wpa = pos;
  550. elems->wpa_len = elen;
  551. } else if (elen >= 5 && pos[3] == 2) {
  552. /* OUI Type 2 - WMM IE */
  553. if (pos[4] == 0) {
  554. elems->wmm_info = pos;
  555. elems->wmm_info_len = elen;
  556. } else if (pos[4] == 1) {
  557. elems->wmm_param = pos;
  558. elems->wmm_param_len = elen;
  559. }
  560. }
  561. }
  562. break;
  563. case WLAN_EID_RSN:
  564. elems->rsn = pos;
  565. elems->rsn_len = elen;
  566. break;
  567. case WLAN_EID_ERP_INFO:
  568. elems->erp_info = pos;
  569. elems->erp_info_len = elen;
  570. break;
  571. case WLAN_EID_EXT_SUPP_RATES:
  572. elems->ext_supp_rates = pos;
  573. elems->ext_supp_rates_len = elen;
  574. break;
  575. case WLAN_EID_HT_CAPABILITY:
  576. if (elen >= sizeof(struct ieee80211_ht_cap))
  577. elems->ht_cap_elem = (void *)pos;
  578. else
  579. elem_parse_failed = true;
  580. break;
  581. case WLAN_EID_HT_INFORMATION:
  582. if (elen >= sizeof(struct ieee80211_ht_info))
  583. elems->ht_info_elem = (void *)pos;
  584. else
  585. elem_parse_failed = true;
  586. break;
  587. case WLAN_EID_MESH_ID:
  588. elems->mesh_id = pos;
  589. elems->mesh_id_len = elen;
  590. break;
  591. case WLAN_EID_MESH_CONFIG:
  592. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  593. elems->mesh_config = (void *)pos;
  594. else
  595. elem_parse_failed = true;
  596. break;
  597. case WLAN_EID_PEER_MGMT:
  598. elems->peering = pos;
  599. elems->peering_len = elen;
  600. break;
  601. case WLAN_EID_PREQ:
  602. elems->preq = pos;
  603. elems->preq_len = elen;
  604. break;
  605. case WLAN_EID_PREP:
  606. elems->prep = pos;
  607. elems->prep_len = elen;
  608. break;
  609. case WLAN_EID_PERR:
  610. elems->perr = pos;
  611. elems->perr_len = elen;
  612. break;
  613. case WLAN_EID_RANN:
  614. if (elen >= sizeof(struct ieee80211_rann_ie))
  615. elems->rann = (void *)pos;
  616. else
  617. elem_parse_failed = true;
  618. break;
  619. case WLAN_EID_CHANNEL_SWITCH:
  620. elems->ch_switch_elem = pos;
  621. elems->ch_switch_elem_len = elen;
  622. break;
  623. case WLAN_EID_QUIET:
  624. if (!elems->quiet_elem) {
  625. elems->quiet_elem = pos;
  626. elems->quiet_elem_len = elen;
  627. }
  628. elems->num_of_quiet_elem++;
  629. break;
  630. case WLAN_EID_COUNTRY:
  631. elems->country_elem = pos;
  632. elems->country_elem_len = elen;
  633. break;
  634. case WLAN_EID_PWR_CONSTRAINT:
  635. elems->pwr_constr_elem = pos;
  636. elems->pwr_constr_elem_len = elen;
  637. break;
  638. case WLAN_EID_TIMEOUT_INTERVAL:
  639. elems->timeout_int = pos;
  640. elems->timeout_int_len = elen;
  641. break;
  642. default:
  643. break;
  644. }
  645. if (elem_parse_failed)
  646. elems->parse_error = true;
  647. else
  648. set_bit(id, seen_elems);
  649. left -= elen;
  650. pos += elen;
  651. }
  652. if (left != 0)
  653. elems->parse_error = true;
  654. return crc;
  655. }
  656. void ieee802_11_parse_elems(u8 *start, size_t len,
  657. struct ieee802_11_elems *elems)
  658. {
  659. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  660. }
  661. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  662. bool bss_notify)
  663. {
  664. struct ieee80211_local *local = sdata->local;
  665. struct ieee80211_tx_queue_params qparam;
  666. int queue;
  667. bool use_11b;
  668. int aCWmin, aCWmax;
  669. if (!local->ops->conf_tx)
  670. return;
  671. memset(&qparam, 0, sizeof(qparam));
  672. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  673. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  674. for (queue = 0; queue < local->hw.queues; queue++) {
  675. /* Set defaults according to 802.11-2007 Table 7-37 */
  676. aCWmax = 1023;
  677. if (use_11b)
  678. aCWmin = 31;
  679. else
  680. aCWmin = 15;
  681. switch (queue) {
  682. case 3: /* AC_BK */
  683. qparam.cw_max = aCWmax;
  684. qparam.cw_min = aCWmin;
  685. qparam.txop = 0;
  686. qparam.aifs = 7;
  687. break;
  688. default: /* never happens but let's not leave undefined */
  689. case 2: /* AC_BE */
  690. qparam.cw_max = aCWmax;
  691. qparam.cw_min = aCWmin;
  692. qparam.txop = 0;
  693. qparam.aifs = 3;
  694. break;
  695. case 1: /* AC_VI */
  696. qparam.cw_max = aCWmin;
  697. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  698. if (use_11b)
  699. qparam.txop = 6016/32;
  700. else
  701. qparam.txop = 3008/32;
  702. qparam.aifs = 2;
  703. break;
  704. case 0: /* AC_VO */
  705. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  706. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  707. if (use_11b)
  708. qparam.txop = 3264/32;
  709. else
  710. qparam.txop = 1504/32;
  711. qparam.aifs = 2;
  712. break;
  713. }
  714. qparam.uapsd = false;
  715. sdata->tx_conf[queue] = qparam;
  716. drv_conf_tx(local, sdata, queue, &qparam);
  717. }
  718. /* after reinitialize QoS TX queues setting to default,
  719. * disable QoS at all */
  720. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  721. sdata->vif.bss_conf.qos =
  722. sdata->vif.type != NL80211_IFTYPE_STATION;
  723. if (bss_notify)
  724. ieee80211_bss_info_change_notify(sdata,
  725. BSS_CHANGED_QOS);
  726. }
  727. }
  728. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  729. const size_t supp_rates_len,
  730. const u8 *supp_rates)
  731. {
  732. struct ieee80211_local *local = sdata->local;
  733. int i, have_higher_than_11mbit = 0;
  734. /* cf. IEEE 802.11 9.2.12 */
  735. for (i = 0; i < supp_rates_len; i++)
  736. if ((supp_rates[i] & 0x7f) * 5 > 110)
  737. have_higher_than_11mbit = 1;
  738. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  739. have_higher_than_11mbit)
  740. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  741. else
  742. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  743. ieee80211_set_wmm_default(sdata, true);
  744. }
  745. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  746. enum ieee80211_band band)
  747. {
  748. struct ieee80211_supported_band *sband;
  749. struct ieee80211_rate *bitrates;
  750. u32 mandatory_rates;
  751. enum ieee80211_rate_flags mandatory_flag;
  752. int i;
  753. sband = local->hw.wiphy->bands[band];
  754. if (!sband) {
  755. WARN_ON(1);
  756. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  757. }
  758. if (band == IEEE80211_BAND_2GHZ)
  759. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  760. else
  761. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  762. bitrates = sband->bitrates;
  763. mandatory_rates = 0;
  764. for (i = 0; i < sband->n_bitrates; i++)
  765. if (bitrates[i].flags & mandatory_flag)
  766. mandatory_rates |= BIT(i);
  767. return mandatory_rates;
  768. }
  769. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  770. u16 transaction, u16 auth_alg,
  771. u8 *extra, size_t extra_len, const u8 *da,
  772. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx)
  773. {
  774. struct ieee80211_local *local = sdata->local;
  775. struct sk_buff *skb;
  776. struct ieee80211_mgmt *mgmt;
  777. int err;
  778. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  779. sizeof(*mgmt) + 6 + extra_len);
  780. if (!skb)
  781. return;
  782. skb_reserve(skb, local->hw.extra_tx_headroom);
  783. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  784. memset(mgmt, 0, 24 + 6);
  785. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  786. IEEE80211_STYPE_AUTH);
  787. memcpy(mgmt->da, da, ETH_ALEN);
  788. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  789. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  790. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  791. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  792. mgmt->u.auth.status_code = cpu_to_le16(0);
  793. if (extra)
  794. memcpy(skb_put(skb, extra_len), extra, extra_len);
  795. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  796. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  797. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  798. WARN_ON(err);
  799. }
  800. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  801. ieee80211_tx_skb(sdata, skb);
  802. }
  803. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  804. const u8 *ie, size_t ie_len,
  805. enum ieee80211_band band, u32 rate_mask,
  806. u8 channel)
  807. {
  808. struct ieee80211_supported_band *sband;
  809. u8 *pos;
  810. size_t offset = 0, noffset;
  811. int supp_rates_len, i;
  812. u8 rates[32];
  813. int num_rates;
  814. int ext_rates_len;
  815. sband = local->hw.wiphy->bands[band];
  816. pos = buffer;
  817. num_rates = 0;
  818. for (i = 0; i < sband->n_bitrates; i++) {
  819. if ((BIT(i) & rate_mask) == 0)
  820. continue; /* skip rate */
  821. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  822. }
  823. supp_rates_len = min_t(int, num_rates, 8);
  824. *pos++ = WLAN_EID_SUPP_RATES;
  825. *pos++ = supp_rates_len;
  826. memcpy(pos, rates, supp_rates_len);
  827. pos += supp_rates_len;
  828. /* insert "request information" if in custom IEs */
  829. if (ie && ie_len) {
  830. static const u8 before_extrates[] = {
  831. WLAN_EID_SSID,
  832. WLAN_EID_SUPP_RATES,
  833. WLAN_EID_REQUEST,
  834. };
  835. noffset = ieee80211_ie_split(ie, ie_len,
  836. before_extrates,
  837. ARRAY_SIZE(before_extrates),
  838. offset);
  839. memcpy(pos, ie + offset, noffset - offset);
  840. pos += noffset - offset;
  841. offset = noffset;
  842. }
  843. ext_rates_len = num_rates - supp_rates_len;
  844. if (ext_rates_len > 0) {
  845. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  846. *pos++ = ext_rates_len;
  847. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  848. pos += ext_rates_len;
  849. }
  850. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  851. *pos++ = WLAN_EID_DS_PARAMS;
  852. *pos++ = 1;
  853. *pos++ = channel;
  854. }
  855. /* insert custom IEs that go before HT */
  856. if (ie && ie_len) {
  857. static const u8 before_ht[] = {
  858. WLAN_EID_SSID,
  859. WLAN_EID_SUPP_RATES,
  860. WLAN_EID_REQUEST,
  861. WLAN_EID_EXT_SUPP_RATES,
  862. WLAN_EID_DS_PARAMS,
  863. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  864. };
  865. noffset = ieee80211_ie_split(ie, ie_len,
  866. before_ht, ARRAY_SIZE(before_ht),
  867. offset);
  868. memcpy(pos, ie + offset, noffset - offset);
  869. pos += noffset - offset;
  870. offset = noffset;
  871. }
  872. if (sband->ht_cap.ht_supported)
  873. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  874. sband->ht_cap.cap);
  875. /*
  876. * If adding more here, adjust code in main.c
  877. * that calculates local->scan_ies_len.
  878. */
  879. /* add any remaining custom IEs */
  880. if (ie && ie_len) {
  881. noffset = ie_len;
  882. memcpy(pos, ie + offset, noffset - offset);
  883. pos += noffset - offset;
  884. }
  885. return pos - buffer;
  886. }
  887. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  888. u8 *dst, u32 ratemask,
  889. const u8 *ssid, size_t ssid_len,
  890. const u8 *ie, size_t ie_len,
  891. bool directed)
  892. {
  893. struct ieee80211_local *local = sdata->local;
  894. struct sk_buff *skb;
  895. struct ieee80211_mgmt *mgmt;
  896. size_t buf_len;
  897. u8 *buf;
  898. u8 chan;
  899. /* FIXME: come up with a proper value */
  900. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  901. if (!buf)
  902. return NULL;
  903. /*
  904. * Do not send DS Channel parameter for directed probe requests
  905. * in order to maximize the chance that we get a response. Some
  906. * badly-behaved APs don't respond when this parameter is included.
  907. */
  908. if (directed)
  909. chan = 0;
  910. else
  911. chan = ieee80211_frequency_to_channel(
  912. local->hw.conf.channel->center_freq);
  913. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  914. local->hw.conf.channel->band,
  915. ratemask, chan);
  916. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  917. ssid, ssid_len,
  918. buf, buf_len);
  919. if (!skb)
  920. goto out;
  921. if (dst) {
  922. mgmt = (struct ieee80211_mgmt *) skb->data;
  923. memcpy(mgmt->da, dst, ETH_ALEN);
  924. memcpy(mgmt->bssid, dst, ETH_ALEN);
  925. }
  926. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  927. out:
  928. kfree(buf);
  929. return skb;
  930. }
  931. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  932. const u8 *ssid, size_t ssid_len,
  933. const u8 *ie, size_t ie_len,
  934. u32 ratemask, bool directed, bool no_cck)
  935. {
  936. struct sk_buff *skb;
  937. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  938. ie, ie_len, directed);
  939. if (skb) {
  940. if (no_cck)
  941. IEEE80211_SKB_CB(skb)->flags |=
  942. IEEE80211_TX_CTL_NO_CCK_RATE;
  943. ieee80211_tx_skb(sdata, skb);
  944. }
  945. }
  946. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  947. struct ieee802_11_elems *elems,
  948. enum ieee80211_band band)
  949. {
  950. struct ieee80211_supported_band *sband;
  951. struct ieee80211_rate *bitrates;
  952. size_t num_rates;
  953. u32 supp_rates;
  954. int i, j;
  955. sband = local->hw.wiphy->bands[band];
  956. if (!sband) {
  957. WARN_ON(1);
  958. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  959. }
  960. bitrates = sband->bitrates;
  961. num_rates = sband->n_bitrates;
  962. supp_rates = 0;
  963. for (i = 0; i < elems->supp_rates_len +
  964. elems->ext_supp_rates_len; i++) {
  965. u8 rate = 0;
  966. int own_rate;
  967. if (i < elems->supp_rates_len)
  968. rate = elems->supp_rates[i];
  969. else if (elems->ext_supp_rates)
  970. rate = elems->ext_supp_rates
  971. [i - elems->supp_rates_len];
  972. own_rate = 5 * (rate & 0x7f);
  973. for (j = 0; j < num_rates; j++)
  974. if (bitrates[j].bitrate == own_rate)
  975. supp_rates |= BIT(j);
  976. }
  977. return supp_rates;
  978. }
  979. void ieee80211_stop_device(struct ieee80211_local *local)
  980. {
  981. ieee80211_led_radio(local, false);
  982. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  983. cancel_work_sync(&local->reconfig_filter);
  984. flush_workqueue(local->workqueue);
  985. drv_stop(local);
  986. }
  987. int ieee80211_reconfig(struct ieee80211_local *local)
  988. {
  989. struct ieee80211_hw *hw = &local->hw;
  990. struct ieee80211_sub_if_data *sdata;
  991. struct sta_info *sta;
  992. int res, i;
  993. #ifdef CONFIG_PM
  994. if (local->suspended)
  995. local->resuming = true;
  996. if (local->wowlan) {
  997. local->wowlan = false;
  998. res = drv_resume(local);
  999. if (res < 0) {
  1000. local->resuming = false;
  1001. return res;
  1002. }
  1003. if (res == 0)
  1004. goto wake_up;
  1005. WARN_ON(res > 1);
  1006. /*
  1007. * res is 1, which means the driver requested
  1008. * to go through a regular reset on wakeup.
  1009. */
  1010. }
  1011. #endif
  1012. /* everything else happens only if HW was up & running */
  1013. if (!local->open_count)
  1014. goto wake_up;
  1015. /*
  1016. * Upon resume hardware can sometimes be goofy due to
  1017. * various platform / driver / bus issues, so restarting
  1018. * the device may at times not work immediately. Propagate
  1019. * the error.
  1020. */
  1021. res = drv_start(local);
  1022. if (res) {
  1023. WARN(local->suspended, "Hardware became unavailable "
  1024. "upon resume. This could be a software issue "
  1025. "prior to suspend or a hardware issue.\n");
  1026. return res;
  1027. }
  1028. /* setup fragmentation threshold */
  1029. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1030. /* setup RTS threshold */
  1031. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1032. /* reset coverage class */
  1033. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1034. ieee80211_led_radio(local, true);
  1035. ieee80211_mod_tpt_led_trig(local,
  1036. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1037. /* add interfaces */
  1038. list_for_each_entry(sdata, &local->interfaces, list) {
  1039. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1040. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1041. ieee80211_sdata_running(sdata))
  1042. res = drv_add_interface(local, sdata);
  1043. }
  1044. /* add STAs back */
  1045. mutex_lock(&local->sta_mtx);
  1046. list_for_each_entry(sta, &local->sta_list, list) {
  1047. if (sta->uploaded) {
  1048. enum ieee80211_sta_state state;
  1049. for (state = IEEE80211_STA_NOTEXIST;
  1050. state < sta->sta_state - 1; state++)
  1051. WARN_ON(drv_sta_state(local, sta->sdata, sta,
  1052. state, state + 1));
  1053. }
  1054. }
  1055. mutex_unlock(&local->sta_mtx);
  1056. /* reconfigure tx conf */
  1057. list_for_each_entry(sdata, &local->interfaces, list) {
  1058. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1059. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1060. !ieee80211_sdata_running(sdata))
  1061. continue;
  1062. for (i = 0; i < hw->queues; i++)
  1063. drv_conf_tx(local, sdata, i, &sdata->tx_conf[i]);
  1064. }
  1065. /* reconfigure hardware */
  1066. ieee80211_hw_config(local, ~0);
  1067. ieee80211_configure_filter(local);
  1068. /* Finally also reconfigure all the BSS information */
  1069. list_for_each_entry(sdata, &local->interfaces, list) {
  1070. u32 changed;
  1071. if (!ieee80211_sdata_running(sdata))
  1072. continue;
  1073. /* common change flags for all interface types */
  1074. changed = BSS_CHANGED_ERP_CTS_PROT |
  1075. BSS_CHANGED_ERP_PREAMBLE |
  1076. BSS_CHANGED_ERP_SLOT |
  1077. BSS_CHANGED_HT |
  1078. BSS_CHANGED_BASIC_RATES |
  1079. BSS_CHANGED_BEACON_INT |
  1080. BSS_CHANGED_BSSID |
  1081. BSS_CHANGED_CQM |
  1082. BSS_CHANGED_QOS |
  1083. BSS_CHANGED_IDLE;
  1084. switch (sdata->vif.type) {
  1085. case NL80211_IFTYPE_STATION:
  1086. changed |= BSS_CHANGED_ASSOC |
  1087. BSS_CHANGED_ARP_FILTER;
  1088. mutex_lock(&sdata->u.mgd.mtx);
  1089. ieee80211_bss_info_change_notify(sdata, changed);
  1090. mutex_unlock(&sdata->u.mgd.mtx);
  1091. break;
  1092. case NL80211_IFTYPE_ADHOC:
  1093. changed |= BSS_CHANGED_IBSS;
  1094. /* fall through */
  1095. case NL80211_IFTYPE_AP:
  1096. changed |= BSS_CHANGED_SSID;
  1097. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1098. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1099. /* fall through */
  1100. case NL80211_IFTYPE_MESH_POINT:
  1101. changed |= BSS_CHANGED_BEACON |
  1102. BSS_CHANGED_BEACON_ENABLED;
  1103. ieee80211_bss_info_change_notify(sdata, changed);
  1104. break;
  1105. case NL80211_IFTYPE_WDS:
  1106. break;
  1107. case NL80211_IFTYPE_AP_VLAN:
  1108. case NL80211_IFTYPE_MONITOR:
  1109. /* ignore virtual */
  1110. break;
  1111. case NL80211_IFTYPE_UNSPECIFIED:
  1112. case NUM_NL80211_IFTYPES:
  1113. case NL80211_IFTYPE_P2P_CLIENT:
  1114. case NL80211_IFTYPE_P2P_GO:
  1115. WARN_ON(1);
  1116. break;
  1117. }
  1118. }
  1119. ieee80211_recalc_ps(local, -1);
  1120. /*
  1121. * The sta might be in psm against the ap (e.g. because
  1122. * this was the state before a hw restart), so we
  1123. * explicitly send a null packet in order to make sure
  1124. * it'll sync against the ap (and get out of psm).
  1125. */
  1126. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1127. list_for_each_entry(sdata, &local->interfaces, list) {
  1128. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1129. continue;
  1130. ieee80211_send_nullfunc(local, sdata, 0);
  1131. }
  1132. }
  1133. /*
  1134. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1135. * sessions can be established after a resume.
  1136. *
  1137. * Also tear down aggregation sessions since reconfiguring
  1138. * them in a hardware restart scenario is not easily done
  1139. * right now, and the hardware will have lost information
  1140. * about the sessions, but we and the AP still think they
  1141. * are active. This is really a workaround though.
  1142. */
  1143. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1144. mutex_lock(&local->sta_mtx);
  1145. list_for_each_entry(sta, &local->sta_list, list) {
  1146. ieee80211_sta_tear_down_BA_sessions(sta, true);
  1147. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1148. }
  1149. mutex_unlock(&local->sta_mtx);
  1150. }
  1151. /* add back keys */
  1152. list_for_each_entry(sdata, &local->interfaces, list)
  1153. if (ieee80211_sdata_running(sdata))
  1154. ieee80211_enable_keys(sdata);
  1155. wake_up:
  1156. ieee80211_wake_queues_by_reason(hw,
  1157. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1158. /*
  1159. * If this is for hw restart things are still running.
  1160. * We may want to change that later, however.
  1161. */
  1162. if (!local->suspended)
  1163. return 0;
  1164. #ifdef CONFIG_PM
  1165. /* first set suspended false, then resuming */
  1166. local->suspended = false;
  1167. mb();
  1168. local->resuming = false;
  1169. list_for_each_entry(sdata, &local->interfaces, list) {
  1170. switch(sdata->vif.type) {
  1171. case NL80211_IFTYPE_STATION:
  1172. ieee80211_sta_restart(sdata);
  1173. break;
  1174. case NL80211_IFTYPE_ADHOC:
  1175. ieee80211_ibss_restart(sdata);
  1176. break;
  1177. case NL80211_IFTYPE_MESH_POINT:
  1178. ieee80211_mesh_restart(sdata);
  1179. break;
  1180. default:
  1181. break;
  1182. }
  1183. }
  1184. mod_timer(&local->sta_cleanup, jiffies + 1);
  1185. mutex_lock(&local->sta_mtx);
  1186. list_for_each_entry(sta, &local->sta_list, list)
  1187. mesh_plink_restart(sta);
  1188. mutex_unlock(&local->sta_mtx);
  1189. #else
  1190. WARN_ON(1);
  1191. #endif
  1192. return 0;
  1193. }
  1194. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1195. {
  1196. struct ieee80211_sub_if_data *sdata;
  1197. struct ieee80211_local *local;
  1198. struct ieee80211_key *key;
  1199. if (WARN_ON(!vif))
  1200. return;
  1201. sdata = vif_to_sdata(vif);
  1202. local = sdata->local;
  1203. if (WARN_ON(!local->resuming))
  1204. return;
  1205. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1206. return;
  1207. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1208. mutex_lock(&local->key_mtx);
  1209. list_for_each_entry(key, &sdata->key_list, list)
  1210. key->flags |= KEY_FLAG_TAINTED;
  1211. mutex_unlock(&local->key_mtx);
  1212. }
  1213. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1214. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1215. enum ieee80211_smps_mode *smps_mode)
  1216. {
  1217. if (ifmgd->associated) {
  1218. *smps_mode = ifmgd->ap_smps;
  1219. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1220. if (ifmgd->powersave)
  1221. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1222. else
  1223. *smps_mode = IEEE80211_SMPS_OFF;
  1224. }
  1225. return 1;
  1226. }
  1227. return 0;
  1228. }
  1229. /* must hold iflist_mtx */
  1230. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1231. {
  1232. struct ieee80211_sub_if_data *sdata;
  1233. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1234. int count = 0;
  1235. lockdep_assert_held(&local->iflist_mtx);
  1236. /*
  1237. * This function could be improved to handle multiple
  1238. * interfaces better, but right now it makes any
  1239. * non-station interfaces force SM PS to be turned
  1240. * off. If there are multiple station interfaces it
  1241. * could also use the best possible mode, e.g. if
  1242. * one is in static and the other in dynamic then
  1243. * dynamic is ok.
  1244. */
  1245. list_for_each_entry(sdata, &local->interfaces, list) {
  1246. if (!ieee80211_sdata_running(sdata))
  1247. continue;
  1248. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1249. goto set;
  1250. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1251. if (count > 1) {
  1252. smps_mode = IEEE80211_SMPS_OFF;
  1253. break;
  1254. }
  1255. }
  1256. if (smps_mode == local->smps_mode)
  1257. return;
  1258. set:
  1259. local->smps_mode = smps_mode;
  1260. /* changed flag is auto-detected for this */
  1261. ieee80211_hw_config(local, 0);
  1262. }
  1263. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1264. {
  1265. int i;
  1266. for (i = 0; i < n_ids; i++)
  1267. if (ids[i] == id)
  1268. return true;
  1269. return false;
  1270. }
  1271. /**
  1272. * ieee80211_ie_split - split an IE buffer according to ordering
  1273. *
  1274. * @ies: the IE buffer
  1275. * @ielen: the length of the IE buffer
  1276. * @ids: an array with element IDs that are allowed before
  1277. * the split
  1278. * @n_ids: the size of the element ID array
  1279. * @offset: offset where to start splitting in the buffer
  1280. *
  1281. * This function splits an IE buffer by updating the @offset
  1282. * variable to point to the location where the buffer should be
  1283. * split.
  1284. *
  1285. * It assumes that the given IE buffer is well-formed, this
  1286. * has to be guaranteed by the caller!
  1287. *
  1288. * It also assumes that the IEs in the buffer are ordered
  1289. * correctly, if not the result of using this function will not
  1290. * be ordered correctly either, i.e. it does no reordering.
  1291. *
  1292. * The function returns the offset where the next part of the
  1293. * buffer starts, which may be @ielen if the entire (remainder)
  1294. * of the buffer should be used.
  1295. */
  1296. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1297. const u8 *ids, int n_ids, size_t offset)
  1298. {
  1299. size_t pos = offset;
  1300. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1301. pos += 2 + ies[pos + 1];
  1302. return pos;
  1303. }
  1304. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1305. {
  1306. size_t pos = offset;
  1307. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1308. pos += 2 + ies[pos + 1];
  1309. return pos;
  1310. }
  1311. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1312. int rssi_min_thold,
  1313. int rssi_max_thold)
  1314. {
  1315. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1316. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1317. return;
  1318. /*
  1319. * Scale up threshold values before storing it, as the RSSI averaging
  1320. * algorithm uses a scaled up value as well. Change this scaling
  1321. * factor if the RSSI averaging algorithm changes.
  1322. */
  1323. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1324. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1325. }
  1326. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1327. int rssi_min_thold,
  1328. int rssi_max_thold)
  1329. {
  1330. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1331. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1332. rssi_min_thold > rssi_max_thold);
  1333. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1334. rssi_max_thold);
  1335. }
  1336. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1337. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1338. {
  1339. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1340. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1341. }
  1342. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1343. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1344. u16 cap)
  1345. {
  1346. __le16 tmp;
  1347. *pos++ = WLAN_EID_HT_CAPABILITY;
  1348. *pos++ = sizeof(struct ieee80211_ht_cap);
  1349. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1350. /* capability flags */
  1351. tmp = cpu_to_le16(cap);
  1352. memcpy(pos, &tmp, sizeof(u16));
  1353. pos += sizeof(u16);
  1354. /* AMPDU parameters */
  1355. *pos++ = ht_cap->ampdu_factor |
  1356. (ht_cap->ampdu_density <<
  1357. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1358. /* MCS set */
  1359. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1360. pos += sizeof(ht_cap->mcs);
  1361. /* extended capabilities */
  1362. pos += sizeof(__le16);
  1363. /* BF capabilities */
  1364. pos += sizeof(__le32);
  1365. /* antenna selection */
  1366. pos += sizeof(u8);
  1367. return pos;
  1368. }
  1369. u8 *ieee80211_ie_build_ht_info(u8 *pos,
  1370. struct ieee80211_sta_ht_cap *ht_cap,
  1371. struct ieee80211_channel *channel,
  1372. enum nl80211_channel_type channel_type)
  1373. {
  1374. struct ieee80211_ht_info *ht_info;
  1375. /* Build HT Information */
  1376. *pos++ = WLAN_EID_HT_INFORMATION;
  1377. *pos++ = sizeof(struct ieee80211_ht_info);
  1378. ht_info = (struct ieee80211_ht_info *)pos;
  1379. ht_info->control_chan =
  1380. ieee80211_frequency_to_channel(channel->center_freq);
  1381. switch (channel_type) {
  1382. case NL80211_CHAN_HT40MINUS:
  1383. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1384. break;
  1385. case NL80211_CHAN_HT40PLUS:
  1386. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1387. break;
  1388. case NL80211_CHAN_HT20:
  1389. default:
  1390. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1391. break;
  1392. }
  1393. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
  1394. ht_info->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1395. /*
  1396. * Note: According to 802.11n-2009 9.13.3.1, HT Protection field and
  1397. * RIFS Mode are reserved in IBSS mode, therefore keep them at 0
  1398. */
  1399. ht_info->operation_mode = 0x0000;
  1400. ht_info->stbc_param = 0x0000;
  1401. /* It seems that Basic MCS set and Supported MCS set
  1402. are identical for the first 10 bytes */
  1403. memset(&ht_info->basic_set, 0, 16);
  1404. memcpy(&ht_info->basic_set, &ht_cap->mcs, 10);
  1405. return pos + sizeof(struct ieee80211_ht_info);
  1406. }
  1407. enum nl80211_channel_type
  1408. ieee80211_ht_info_to_channel_type(struct ieee80211_ht_info *ht_info)
  1409. {
  1410. enum nl80211_channel_type channel_type;
  1411. if (!ht_info)
  1412. return NL80211_CHAN_NO_HT;
  1413. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1414. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1415. channel_type = NL80211_CHAN_HT20;
  1416. break;
  1417. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1418. channel_type = NL80211_CHAN_HT40PLUS;
  1419. break;
  1420. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1421. channel_type = NL80211_CHAN_HT40MINUS;
  1422. break;
  1423. default:
  1424. channel_type = NL80211_CHAN_NO_HT;
  1425. }
  1426. return channel_type;
  1427. }
  1428. int ieee80211_add_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1429. {
  1430. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1431. struct ieee80211_local *local = sdata->local;
  1432. struct ieee80211_supported_band *sband;
  1433. int rate;
  1434. u8 i, rates, *pos;
  1435. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1436. rates = sband->n_bitrates;
  1437. if (rates > 8)
  1438. rates = 8;
  1439. if (skb_tailroom(skb) < rates + 2)
  1440. return -ENOMEM;
  1441. pos = skb_put(skb, rates + 2);
  1442. *pos++ = WLAN_EID_SUPP_RATES;
  1443. *pos++ = rates;
  1444. for (i = 0; i < rates; i++) {
  1445. rate = sband->bitrates[i].bitrate;
  1446. *pos++ = (u8) (rate / 5);
  1447. }
  1448. return 0;
  1449. }
  1450. int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1451. {
  1452. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1453. struct ieee80211_local *local = sdata->local;
  1454. struct ieee80211_supported_band *sband;
  1455. int rate;
  1456. u8 i, exrates, *pos;
  1457. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1458. exrates = sband->n_bitrates;
  1459. if (exrates > 8)
  1460. exrates -= 8;
  1461. else
  1462. exrates = 0;
  1463. if (skb_tailroom(skb) < exrates + 2)
  1464. return -ENOMEM;
  1465. if (exrates) {
  1466. pos = skb_put(skb, exrates + 2);
  1467. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1468. *pos++ = exrates;
  1469. for (i = 8; i < sband->n_bitrates; i++) {
  1470. rate = sband->bitrates[i].bitrate;
  1471. *pos++ = (u8) (rate / 5);
  1472. }
  1473. }
  1474. return 0;
  1475. }