util.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/bitmap.h>
  21. #include <net/net_namespace.h>
  22. #include <net/cfg80211.h>
  23. #include <net/rtnetlink.h>
  24. #include "ieee80211_i.h"
  25. #include "driver-ops.h"
  26. #include "rate.h"
  27. #include "mesh.h"
  28. #include "wme.h"
  29. #include "led.h"
  30. #include "wep.h"
  31. /* privid for wiphys to determine whether they belong to us or not */
  32. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  33. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  34. {
  35. struct ieee80211_local *local;
  36. BUG_ON(!wiphy);
  37. local = wiphy_priv(wiphy);
  38. return &local->hw;
  39. }
  40. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  41. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  42. enum nl80211_iftype type)
  43. {
  44. __le16 fc = hdr->frame_control;
  45. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  46. if (len < 16)
  47. return NULL;
  48. if (ieee80211_is_data(fc)) {
  49. if (len < 24) /* drop incorrect hdr len (data) */
  50. return NULL;
  51. if (ieee80211_has_a4(fc))
  52. return NULL;
  53. if (ieee80211_has_tods(fc))
  54. return hdr->addr1;
  55. if (ieee80211_has_fromds(fc))
  56. return hdr->addr2;
  57. return hdr->addr3;
  58. }
  59. if (ieee80211_is_mgmt(fc)) {
  60. if (len < 24) /* drop incorrect hdr len (mgmt) */
  61. return NULL;
  62. return hdr->addr3;
  63. }
  64. if (ieee80211_is_ctl(fc)) {
  65. if(ieee80211_is_pspoll(fc))
  66. return hdr->addr1;
  67. if (ieee80211_is_back_req(fc)) {
  68. switch (type) {
  69. case NL80211_IFTYPE_STATION:
  70. return hdr->addr2;
  71. case NL80211_IFTYPE_AP:
  72. case NL80211_IFTYPE_AP_VLAN:
  73. return hdr->addr1;
  74. default:
  75. break; /* fall through to the return */
  76. }
  77. }
  78. }
  79. return NULL;
  80. }
  81. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  82. {
  83. struct sk_buff *skb = tx->skb;
  84. struct ieee80211_hdr *hdr;
  85. do {
  86. hdr = (struct ieee80211_hdr *) skb->data;
  87. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  88. } while ((skb = skb->next));
  89. }
  90. int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  91. int rate, int erp, int short_preamble)
  92. {
  93. int dur;
  94. /* calculate duration (in microseconds, rounded up to next higher
  95. * integer if it includes a fractional microsecond) to send frame of
  96. * len bytes (does not include FCS) at the given rate. Duration will
  97. * also include SIFS.
  98. *
  99. * rate is in 100 kbps, so divident is multiplied by 10 in the
  100. * DIV_ROUND_UP() operations.
  101. */
  102. if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
  103. /*
  104. * OFDM:
  105. *
  106. * N_DBPS = DATARATE x 4
  107. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  108. * (16 = SIGNAL time, 6 = tail bits)
  109. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  110. *
  111. * T_SYM = 4 usec
  112. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  113. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  114. * signal ext = 6 usec
  115. */
  116. dur = 16; /* SIFS + signal ext */
  117. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  118. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  119. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  120. 4 * rate); /* T_SYM x N_SYM */
  121. } else {
  122. /*
  123. * 802.11b or 802.11g with 802.11b compatibility:
  124. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  125. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  126. *
  127. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  128. * aSIFSTime = 10 usec
  129. * aPreambleLength = 144 usec or 72 usec with short preamble
  130. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  131. */
  132. dur = 10; /* aSIFSTime = 10 usec */
  133. dur += short_preamble ? (72 + 24) : (144 + 48);
  134. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  135. }
  136. return dur;
  137. }
  138. /* Exported duration function for driver use */
  139. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  140. struct ieee80211_vif *vif,
  141. size_t frame_len,
  142. struct ieee80211_rate *rate)
  143. {
  144. struct ieee80211_local *local = hw_to_local(hw);
  145. struct ieee80211_sub_if_data *sdata;
  146. u16 dur;
  147. int erp;
  148. bool short_preamble = false;
  149. erp = 0;
  150. if (vif) {
  151. sdata = vif_to_sdata(vif);
  152. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  153. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  154. erp = rate->flags & IEEE80211_RATE_ERP_G;
  155. }
  156. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
  157. short_preamble);
  158. return cpu_to_le16(dur);
  159. }
  160. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  161. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  162. struct ieee80211_vif *vif, size_t frame_len,
  163. const struct ieee80211_tx_info *frame_txctl)
  164. {
  165. struct ieee80211_local *local = hw_to_local(hw);
  166. struct ieee80211_rate *rate;
  167. struct ieee80211_sub_if_data *sdata;
  168. bool short_preamble;
  169. int erp;
  170. u16 dur;
  171. struct ieee80211_supported_band *sband;
  172. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  173. short_preamble = false;
  174. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  175. erp = 0;
  176. if (vif) {
  177. sdata = vif_to_sdata(vif);
  178. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  179. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  180. erp = rate->flags & IEEE80211_RATE_ERP_G;
  181. }
  182. /* CTS duration */
  183. dur = ieee80211_frame_duration(local, 10, rate->bitrate,
  184. erp, short_preamble);
  185. /* Data frame duration */
  186. dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
  187. erp, short_preamble);
  188. /* ACK duration */
  189. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  190. erp, short_preamble);
  191. return cpu_to_le16(dur);
  192. }
  193. EXPORT_SYMBOL(ieee80211_rts_duration);
  194. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  195. struct ieee80211_vif *vif,
  196. size_t frame_len,
  197. const struct ieee80211_tx_info *frame_txctl)
  198. {
  199. struct ieee80211_local *local = hw_to_local(hw);
  200. struct ieee80211_rate *rate;
  201. struct ieee80211_sub_if_data *sdata;
  202. bool short_preamble;
  203. int erp;
  204. u16 dur;
  205. struct ieee80211_supported_band *sband;
  206. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  207. short_preamble = false;
  208. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  209. erp = 0;
  210. if (vif) {
  211. sdata = vif_to_sdata(vif);
  212. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  213. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  214. erp = rate->flags & IEEE80211_RATE_ERP_G;
  215. }
  216. /* Data frame duration */
  217. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
  218. erp, short_preamble);
  219. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  220. /* ACK duration */
  221. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  222. erp, short_preamble);
  223. }
  224. return cpu_to_le16(dur);
  225. }
  226. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  227. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  228. enum queue_stop_reason reason)
  229. {
  230. struct ieee80211_local *local = hw_to_local(hw);
  231. struct ieee80211_sub_if_data *sdata;
  232. trace_wake_queue(local, queue, reason);
  233. if (WARN_ON(queue >= hw->queues))
  234. return;
  235. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  236. if (local->queue_stop_reasons[queue] != 0)
  237. /* someone still has this queue stopped */
  238. return;
  239. if (skb_queue_empty(&local->pending[queue])) {
  240. rcu_read_lock();
  241. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  242. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  243. continue;
  244. netif_wake_subqueue(sdata->dev, queue);
  245. }
  246. rcu_read_unlock();
  247. } else
  248. tasklet_schedule(&local->tx_pending_tasklet);
  249. }
  250. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  251. enum queue_stop_reason reason)
  252. {
  253. struct ieee80211_local *local = hw_to_local(hw);
  254. unsigned long flags;
  255. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  256. __ieee80211_wake_queue(hw, queue, reason);
  257. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  258. }
  259. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  260. {
  261. ieee80211_wake_queue_by_reason(hw, queue,
  262. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  263. }
  264. EXPORT_SYMBOL(ieee80211_wake_queue);
  265. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  266. enum queue_stop_reason reason)
  267. {
  268. struct ieee80211_local *local = hw_to_local(hw);
  269. struct ieee80211_sub_if_data *sdata;
  270. trace_stop_queue(local, queue, reason);
  271. if (WARN_ON(queue >= hw->queues))
  272. return;
  273. __set_bit(reason, &local->queue_stop_reasons[queue]);
  274. rcu_read_lock();
  275. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  276. netif_stop_subqueue(sdata->dev, queue);
  277. rcu_read_unlock();
  278. }
  279. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  280. enum queue_stop_reason reason)
  281. {
  282. struct ieee80211_local *local = hw_to_local(hw);
  283. unsigned long flags;
  284. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  285. __ieee80211_stop_queue(hw, queue, reason);
  286. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  287. }
  288. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  289. {
  290. ieee80211_stop_queue_by_reason(hw, queue,
  291. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  292. }
  293. EXPORT_SYMBOL(ieee80211_stop_queue);
  294. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  295. struct sk_buff *skb)
  296. {
  297. struct ieee80211_hw *hw = &local->hw;
  298. unsigned long flags;
  299. int queue = skb_get_queue_mapping(skb);
  300. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  301. if (WARN_ON(!info->control.vif)) {
  302. kfree_skb(skb);
  303. return;
  304. }
  305. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  306. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  307. __skb_queue_tail(&local->pending[queue], skb);
  308. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  309. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  310. }
  311. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  312. struct sk_buff_head *skbs,
  313. void (*fn)(void *data), void *data)
  314. {
  315. struct ieee80211_hw *hw = &local->hw;
  316. struct sk_buff *skb;
  317. unsigned long flags;
  318. int queue, i;
  319. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  320. for (i = 0; i < hw->queues; i++)
  321. __ieee80211_stop_queue(hw, i,
  322. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  323. while ((skb = skb_dequeue(skbs))) {
  324. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  325. if (WARN_ON(!info->control.vif)) {
  326. kfree_skb(skb);
  327. continue;
  328. }
  329. queue = skb_get_queue_mapping(skb);
  330. __skb_queue_tail(&local->pending[queue], skb);
  331. }
  332. if (fn)
  333. fn(data);
  334. for (i = 0; i < hw->queues; i++)
  335. __ieee80211_wake_queue(hw, i,
  336. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  337. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  338. }
  339. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  340. struct sk_buff_head *skbs)
  341. {
  342. ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
  343. }
  344. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  345. enum queue_stop_reason reason)
  346. {
  347. struct ieee80211_local *local = hw_to_local(hw);
  348. unsigned long flags;
  349. int i;
  350. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  351. for (i = 0; i < hw->queues; i++)
  352. __ieee80211_stop_queue(hw, i, reason);
  353. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  354. }
  355. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  356. {
  357. ieee80211_stop_queues_by_reason(hw,
  358. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  359. }
  360. EXPORT_SYMBOL(ieee80211_stop_queues);
  361. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  362. {
  363. struct ieee80211_local *local = hw_to_local(hw);
  364. unsigned long flags;
  365. int ret;
  366. if (WARN_ON(queue >= hw->queues))
  367. return true;
  368. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  369. ret = !!local->queue_stop_reasons[queue];
  370. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  371. return ret;
  372. }
  373. EXPORT_SYMBOL(ieee80211_queue_stopped);
  374. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  375. enum queue_stop_reason reason)
  376. {
  377. struct ieee80211_local *local = hw_to_local(hw);
  378. unsigned long flags;
  379. int i;
  380. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  381. for (i = 0; i < hw->queues; i++)
  382. __ieee80211_wake_queue(hw, i, reason);
  383. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  384. }
  385. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  386. {
  387. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  388. }
  389. EXPORT_SYMBOL(ieee80211_wake_queues);
  390. void ieee80211_iterate_active_interfaces(
  391. struct ieee80211_hw *hw,
  392. void (*iterator)(void *data, u8 *mac,
  393. struct ieee80211_vif *vif),
  394. void *data)
  395. {
  396. struct ieee80211_local *local = hw_to_local(hw);
  397. struct ieee80211_sub_if_data *sdata;
  398. mutex_lock(&local->iflist_mtx);
  399. list_for_each_entry(sdata, &local->interfaces, list) {
  400. switch (sdata->vif.type) {
  401. case NL80211_IFTYPE_MONITOR:
  402. case NL80211_IFTYPE_AP_VLAN:
  403. continue;
  404. default:
  405. break;
  406. }
  407. if (ieee80211_sdata_running(sdata))
  408. iterator(data, sdata->vif.addr,
  409. &sdata->vif);
  410. }
  411. mutex_unlock(&local->iflist_mtx);
  412. }
  413. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  414. void ieee80211_iterate_active_interfaces_atomic(
  415. struct ieee80211_hw *hw,
  416. void (*iterator)(void *data, u8 *mac,
  417. struct ieee80211_vif *vif),
  418. void *data)
  419. {
  420. struct ieee80211_local *local = hw_to_local(hw);
  421. struct ieee80211_sub_if_data *sdata;
  422. rcu_read_lock();
  423. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  424. switch (sdata->vif.type) {
  425. case NL80211_IFTYPE_MONITOR:
  426. case NL80211_IFTYPE_AP_VLAN:
  427. continue;
  428. default:
  429. break;
  430. }
  431. if (ieee80211_sdata_running(sdata))
  432. iterator(data, sdata->vif.addr,
  433. &sdata->vif);
  434. }
  435. rcu_read_unlock();
  436. }
  437. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  438. /*
  439. * Nothing should have been stuffed into the workqueue during
  440. * the suspend->resume cycle. If this WARN is seen then there
  441. * is a bug with either the driver suspend or something in
  442. * mac80211 stuffing into the workqueue which we haven't yet
  443. * cleared during mac80211's suspend cycle.
  444. */
  445. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  446. {
  447. if (WARN(local->suspended && !local->resuming,
  448. "queueing ieee80211 work while going to suspend\n"))
  449. return false;
  450. return true;
  451. }
  452. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  453. {
  454. struct ieee80211_local *local = hw_to_local(hw);
  455. if (!ieee80211_can_queue_work(local))
  456. return;
  457. queue_work(local->workqueue, work);
  458. }
  459. EXPORT_SYMBOL(ieee80211_queue_work);
  460. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  461. struct delayed_work *dwork,
  462. unsigned long delay)
  463. {
  464. struct ieee80211_local *local = hw_to_local(hw);
  465. if (!ieee80211_can_queue_work(local))
  466. return;
  467. queue_delayed_work(local->workqueue, dwork, delay);
  468. }
  469. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  470. void ieee802_11_parse_elems(u8 *start, size_t len,
  471. struct ieee802_11_elems *elems)
  472. {
  473. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  474. }
  475. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  476. {
  477. struct ieee80211_local *local = sdata->local;
  478. struct ieee80211_tx_queue_params qparam;
  479. int queue;
  480. bool use_11b;
  481. int aCWmin, aCWmax;
  482. if (!local->ops->conf_tx)
  483. return;
  484. memset(&qparam, 0, sizeof(qparam));
  485. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  486. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  487. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  488. /* Set defaults according to 802.11-2007 Table 7-37 */
  489. aCWmax = 1023;
  490. if (use_11b)
  491. aCWmin = 31;
  492. else
  493. aCWmin = 15;
  494. switch (queue) {
  495. case 3: /* AC_BK */
  496. qparam.cw_max = aCWmax;
  497. qparam.cw_min = aCWmin;
  498. qparam.txop = 0;
  499. qparam.aifs = 7;
  500. break;
  501. default: /* never happens but let's not leave undefined */
  502. case 2: /* AC_BE */
  503. qparam.cw_max = aCWmax;
  504. qparam.cw_min = aCWmin;
  505. qparam.txop = 0;
  506. qparam.aifs = 3;
  507. break;
  508. case 1: /* AC_VI */
  509. qparam.cw_max = aCWmin;
  510. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  511. if (use_11b)
  512. qparam.txop = 6016/32;
  513. else
  514. qparam.txop = 3008/32;
  515. qparam.aifs = 2;
  516. break;
  517. case 0: /* AC_VO */
  518. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  519. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  520. if (use_11b)
  521. qparam.txop = 3264/32;
  522. else
  523. qparam.txop = 1504/32;
  524. qparam.aifs = 2;
  525. break;
  526. }
  527. qparam.uapsd = false;
  528. sdata->tx_conf[queue] = qparam;
  529. drv_conf_tx(local, sdata, queue, &qparam);
  530. }
  531. /* after reinitialize QoS TX queues setting to default,
  532. * disable QoS at all */
  533. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  534. sdata->vif.bss_conf.qos =
  535. sdata->vif.type != NL80211_IFTYPE_STATION;
  536. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  537. }
  538. }
  539. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  540. const size_t supp_rates_len,
  541. const u8 *supp_rates)
  542. {
  543. struct ieee80211_local *local = sdata->local;
  544. int i, have_higher_than_11mbit = 0;
  545. /* cf. IEEE 802.11 9.2.12 */
  546. for (i = 0; i < supp_rates_len; i++)
  547. if ((supp_rates[i] & 0x7f) * 5 > 110)
  548. have_higher_than_11mbit = 1;
  549. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  550. have_higher_than_11mbit)
  551. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  552. else
  553. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  554. ieee80211_set_wmm_default(sdata);
  555. }
  556. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  557. enum ieee80211_band band)
  558. {
  559. struct ieee80211_supported_band *sband;
  560. struct ieee80211_rate *bitrates;
  561. u32 mandatory_rates;
  562. enum ieee80211_rate_flags mandatory_flag;
  563. int i;
  564. sband = local->hw.wiphy->bands[band];
  565. if (!sband) {
  566. WARN_ON(1);
  567. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  568. }
  569. if (band == IEEE80211_BAND_2GHZ)
  570. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  571. else
  572. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  573. bitrates = sband->bitrates;
  574. mandatory_rates = 0;
  575. for (i = 0; i < sband->n_bitrates; i++)
  576. if (bitrates[i].flags & mandatory_flag)
  577. mandatory_rates |= BIT(i);
  578. return mandatory_rates;
  579. }
  580. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  581. u16 transaction, u16 auth_alg,
  582. u8 *extra, size_t extra_len, const u8 *bssid,
  583. const u8 *key, u8 key_len, u8 key_idx)
  584. {
  585. struct ieee80211_local *local = sdata->local;
  586. struct sk_buff *skb;
  587. struct ieee80211_mgmt *mgmt;
  588. int err;
  589. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  590. sizeof(*mgmt) + 6 + extra_len);
  591. if (!skb)
  592. return;
  593. skb_reserve(skb, local->hw.extra_tx_headroom);
  594. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  595. memset(mgmt, 0, 24 + 6);
  596. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  597. IEEE80211_STYPE_AUTH);
  598. memcpy(mgmt->da, bssid, ETH_ALEN);
  599. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  600. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  601. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  602. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  603. mgmt->u.auth.status_code = cpu_to_le16(0);
  604. if (extra)
  605. memcpy(skb_put(skb, extra_len), extra, extra_len);
  606. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  607. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  608. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  609. WARN_ON(err);
  610. }
  611. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  612. ieee80211_tx_skb(sdata, skb);
  613. }
  614. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  615. const u8 *ie, size_t ie_len,
  616. enum ieee80211_band band, u32 rate_mask,
  617. u8 channel)
  618. {
  619. struct ieee80211_supported_band *sband;
  620. u8 *pos;
  621. size_t offset = 0, noffset;
  622. int supp_rates_len, i;
  623. u8 rates[32];
  624. int num_rates;
  625. int ext_rates_len;
  626. sband = local->hw.wiphy->bands[band];
  627. pos = buffer;
  628. num_rates = 0;
  629. for (i = 0; i < sband->n_bitrates; i++) {
  630. if ((BIT(i) & rate_mask) == 0)
  631. continue; /* skip rate */
  632. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  633. }
  634. supp_rates_len = min_t(int, num_rates, 8);
  635. *pos++ = WLAN_EID_SUPP_RATES;
  636. *pos++ = supp_rates_len;
  637. memcpy(pos, rates, supp_rates_len);
  638. pos += supp_rates_len;
  639. /* insert "request information" if in custom IEs */
  640. if (ie && ie_len) {
  641. static const u8 before_extrates[] = {
  642. WLAN_EID_SSID,
  643. WLAN_EID_SUPP_RATES,
  644. WLAN_EID_REQUEST,
  645. };
  646. noffset = ieee80211_ie_split(ie, ie_len,
  647. before_extrates,
  648. ARRAY_SIZE(before_extrates),
  649. offset);
  650. memcpy(pos, ie + offset, noffset - offset);
  651. pos += noffset - offset;
  652. offset = noffset;
  653. }
  654. ext_rates_len = num_rates - supp_rates_len;
  655. if (ext_rates_len > 0) {
  656. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  657. *pos++ = ext_rates_len;
  658. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  659. pos += ext_rates_len;
  660. }
  661. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  662. *pos++ = WLAN_EID_DS_PARAMS;
  663. *pos++ = 1;
  664. *pos++ = channel;
  665. }
  666. /* insert custom IEs that go before HT */
  667. if (ie && ie_len) {
  668. static const u8 before_ht[] = {
  669. WLAN_EID_SSID,
  670. WLAN_EID_SUPP_RATES,
  671. WLAN_EID_REQUEST,
  672. WLAN_EID_EXT_SUPP_RATES,
  673. WLAN_EID_DS_PARAMS,
  674. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  675. };
  676. noffset = ieee80211_ie_split(ie, ie_len,
  677. before_ht, ARRAY_SIZE(before_ht),
  678. offset);
  679. memcpy(pos, ie + offset, noffset - offset);
  680. pos += noffset - offset;
  681. offset = noffset;
  682. }
  683. if (sband->ht_cap.ht_supported)
  684. pos = ieee80211_ie_build_ht_cap(pos, sband, sband->ht_cap.cap);
  685. /*
  686. * If adding more here, adjust code in main.c
  687. * that calculates local->scan_ies_len.
  688. */
  689. /* add any remaining custom IEs */
  690. if (ie && ie_len) {
  691. noffset = ie_len;
  692. memcpy(pos, ie + offset, noffset - offset);
  693. pos += noffset - offset;
  694. }
  695. return pos - buffer;
  696. }
  697. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  698. u8 *dst, u32 ratemask,
  699. const u8 *ssid, size_t ssid_len,
  700. const u8 *ie, size_t ie_len,
  701. bool directed)
  702. {
  703. struct ieee80211_local *local = sdata->local;
  704. struct sk_buff *skb;
  705. struct ieee80211_mgmt *mgmt;
  706. size_t buf_len;
  707. u8 *buf;
  708. u8 chan;
  709. /* FIXME: come up with a proper value */
  710. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  711. if (!buf)
  712. return NULL;
  713. /*
  714. * Do not send DS Channel parameter for directed probe requests
  715. * in order to maximize the chance that we get a response. Some
  716. * badly-behaved APs don't respond when this parameter is included.
  717. */
  718. if (directed)
  719. chan = 0;
  720. else
  721. chan = ieee80211_frequency_to_channel(
  722. local->hw.conf.channel->center_freq);
  723. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  724. local->hw.conf.channel->band,
  725. ratemask, chan);
  726. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  727. ssid, ssid_len,
  728. buf, buf_len);
  729. if (dst) {
  730. mgmt = (struct ieee80211_mgmt *) skb->data;
  731. memcpy(mgmt->da, dst, ETH_ALEN);
  732. memcpy(mgmt->bssid, dst, ETH_ALEN);
  733. }
  734. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  735. kfree(buf);
  736. return skb;
  737. }
  738. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  739. const u8 *ssid, size_t ssid_len,
  740. const u8 *ie, size_t ie_len,
  741. u32 ratemask, bool directed, bool no_cck)
  742. {
  743. struct sk_buff *skb;
  744. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  745. ie, ie_len, directed);
  746. if (skb) {
  747. if (no_cck)
  748. IEEE80211_SKB_CB(skb)->flags |=
  749. IEEE80211_TX_CTL_NO_CCK_RATE;
  750. ieee80211_tx_skb(sdata, skb);
  751. }
  752. }
  753. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  754. struct ieee802_11_elems *elems,
  755. enum ieee80211_band band)
  756. {
  757. struct ieee80211_supported_band *sband;
  758. struct ieee80211_rate *bitrates;
  759. size_t num_rates;
  760. u32 supp_rates;
  761. int i, j;
  762. sband = local->hw.wiphy->bands[band];
  763. if (!sband) {
  764. WARN_ON(1);
  765. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  766. }
  767. bitrates = sband->bitrates;
  768. num_rates = sband->n_bitrates;
  769. supp_rates = 0;
  770. for (i = 0; i < elems->supp_rates_len +
  771. elems->ext_supp_rates_len; i++) {
  772. u8 rate = 0;
  773. int own_rate;
  774. if (i < elems->supp_rates_len)
  775. rate = elems->supp_rates[i];
  776. else if (elems->ext_supp_rates)
  777. rate = elems->ext_supp_rates
  778. [i - elems->supp_rates_len];
  779. own_rate = 5 * (rate & 0x7f);
  780. for (j = 0; j < num_rates; j++)
  781. if (bitrates[j].bitrate == own_rate)
  782. supp_rates |= BIT(j);
  783. }
  784. return supp_rates;
  785. }
  786. void ieee80211_stop_device(struct ieee80211_local *local)
  787. {
  788. ieee80211_led_radio(local, false);
  789. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  790. cancel_work_sync(&local->reconfig_filter);
  791. flush_workqueue(local->workqueue);
  792. drv_stop(local);
  793. }
  794. int ieee80211_reconfig(struct ieee80211_local *local)
  795. {
  796. struct ieee80211_hw *hw = &local->hw;
  797. struct ieee80211_sub_if_data *sdata;
  798. struct sta_info *sta;
  799. int res, i;
  800. #ifdef CONFIG_PM
  801. if (local->suspended)
  802. local->resuming = true;
  803. if (local->wowlan) {
  804. local->wowlan = false;
  805. res = drv_resume(local);
  806. if (res < 0) {
  807. local->resuming = false;
  808. return res;
  809. }
  810. if (res == 0)
  811. goto wake_up;
  812. WARN_ON(res > 1);
  813. /*
  814. * res is 1, which means the driver requested
  815. * to go through a regular reset on wakeup.
  816. */
  817. }
  818. #endif
  819. /* setup fragmentation threshold */
  820. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  821. /* setup RTS threshold */
  822. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  823. /* reset coverage class */
  824. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  825. /* everything else happens only if HW was up & running */
  826. if (!local->open_count)
  827. goto wake_up;
  828. /*
  829. * Upon resume hardware can sometimes be goofy due to
  830. * various platform / driver / bus issues, so restarting
  831. * the device may at times not work immediately. Propagate
  832. * the error.
  833. */
  834. res = drv_start(local);
  835. if (res) {
  836. WARN(local->suspended, "Hardware became unavailable "
  837. "upon resume. This could be a software issue "
  838. "prior to suspend or a hardware issue.\n");
  839. return res;
  840. }
  841. ieee80211_led_radio(local, true);
  842. ieee80211_mod_tpt_led_trig(local,
  843. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  844. /* add interfaces */
  845. list_for_each_entry(sdata, &local->interfaces, list) {
  846. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  847. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  848. ieee80211_sdata_running(sdata))
  849. res = drv_add_interface(local, sdata);
  850. }
  851. /* add STAs back */
  852. mutex_lock(&local->sta_mtx);
  853. list_for_each_entry(sta, &local->sta_list, list) {
  854. if (sta->uploaded) {
  855. sdata = sta->sdata;
  856. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  857. sdata = container_of(sdata->bss,
  858. struct ieee80211_sub_if_data,
  859. u.ap);
  860. memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
  861. WARN_ON(drv_sta_add(local, sdata, &sta->sta));
  862. }
  863. }
  864. mutex_unlock(&local->sta_mtx);
  865. /* reconfigure tx conf */
  866. list_for_each_entry(sdata, &local->interfaces, list) {
  867. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  868. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  869. !ieee80211_sdata_running(sdata))
  870. continue;
  871. for (i = 0; i < hw->queues; i++)
  872. drv_conf_tx(local, sdata, i, &sdata->tx_conf[i]);
  873. }
  874. /* reconfigure hardware */
  875. ieee80211_hw_config(local, ~0);
  876. ieee80211_configure_filter(local);
  877. /* Finally also reconfigure all the BSS information */
  878. list_for_each_entry(sdata, &local->interfaces, list) {
  879. u32 changed;
  880. if (!ieee80211_sdata_running(sdata))
  881. continue;
  882. /* common change flags for all interface types */
  883. changed = BSS_CHANGED_ERP_CTS_PROT |
  884. BSS_CHANGED_ERP_PREAMBLE |
  885. BSS_CHANGED_ERP_SLOT |
  886. BSS_CHANGED_HT |
  887. BSS_CHANGED_BASIC_RATES |
  888. BSS_CHANGED_BEACON_INT |
  889. BSS_CHANGED_BSSID |
  890. BSS_CHANGED_CQM |
  891. BSS_CHANGED_QOS |
  892. BSS_CHANGED_IDLE;
  893. switch (sdata->vif.type) {
  894. case NL80211_IFTYPE_STATION:
  895. changed |= BSS_CHANGED_ASSOC;
  896. mutex_lock(&sdata->u.mgd.mtx);
  897. ieee80211_bss_info_change_notify(sdata, changed);
  898. mutex_unlock(&sdata->u.mgd.mtx);
  899. break;
  900. case NL80211_IFTYPE_ADHOC:
  901. changed |= BSS_CHANGED_IBSS;
  902. /* fall through */
  903. case NL80211_IFTYPE_AP:
  904. changed |= BSS_CHANGED_SSID;
  905. /* fall through */
  906. case NL80211_IFTYPE_MESH_POINT:
  907. changed |= BSS_CHANGED_BEACON |
  908. BSS_CHANGED_BEACON_ENABLED;
  909. ieee80211_bss_info_change_notify(sdata, changed);
  910. break;
  911. case NL80211_IFTYPE_WDS:
  912. break;
  913. case NL80211_IFTYPE_AP_VLAN:
  914. case NL80211_IFTYPE_MONITOR:
  915. /* ignore virtual */
  916. break;
  917. case NL80211_IFTYPE_UNSPECIFIED:
  918. case NUM_NL80211_IFTYPES:
  919. case NL80211_IFTYPE_P2P_CLIENT:
  920. case NL80211_IFTYPE_P2P_GO:
  921. WARN_ON(1);
  922. break;
  923. }
  924. }
  925. ieee80211_recalc_ps(local, -1);
  926. /*
  927. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  928. * sessions can be established after a resume.
  929. *
  930. * Also tear down aggregation sessions since reconfiguring
  931. * them in a hardware restart scenario is not easily done
  932. * right now, and the hardware will have lost information
  933. * about the sessions, but we and the AP still think they
  934. * are active. This is really a workaround though.
  935. */
  936. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  937. mutex_lock(&local->sta_mtx);
  938. list_for_each_entry(sta, &local->sta_list, list) {
  939. ieee80211_sta_tear_down_BA_sessions(sta, true);
  940. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  941. }
  942. mutex_unlock(&local->sta_mtx);
  943. }
  944. /* add back keys */
  945. list_for_each_entry(sdata, &local->interfaces, list)
  946. if (ieee80211_sdata_running(sdata))
  947. ieee80211_enable_keys(sdata);
  948. wake_up:
  949. ieee80211_wake_queues_by_reason(hw,
  950. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  951. /*
  952. * If this is for hw restart things are still running.
  953. * We may want to change that later, however.
  954. */
  955. if (!local->suspended)
  956. return 0;
  957. #ifdef CONFIG_PM
  958. /* first set suspended false, then resuming */
  959. local->suspended = false;
  960. mb();
  961. local->resuming = false;
  962. list_for_each_entry(sdata, &local->interfaces, list) {
  963. switch(sdata->vif.type) {
  964. case NL80211_IFTYPE_STATION:
  965. ieee80211_sta_restart(sdata);
  966. break;
  967. case NL80211_IFTYPE_ADHOC:
  968. ieee80211_ibss_restart(sdata);
  969. break;
  970. case NL80211_IFTYPE_MESH_POINT:
  971. ieee80211_mesh_restart(sdata);
  972. break;
  973. default:
  974. break;
  975. }
  976. }
  977. mod_timer(&local->sta_cleanup, jiffies + 1);
  978. mutex_lock(&local->sta_mtx);
  979. list_for_each_entry(sta, &local->sta_list, list)
  980. mesh_plink_restart(sta);
  981. mutex_unlock(&local->sta_mtx);
  982. #else
  983. WARN_ON(1);
  984. #endif
  985. return 0;
  986. }
  987. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  988. {
  989. struct ieee80211_sub_if_data *sdata;
  990. struct ieee80211_local *local;
  991. struct ieee80211_key *key;
  992. if (WARN_ON(!vif))
  993. return;
  994. sdata = vif_to_sdata(vif);
  995. local = sdata->local;
  996. if (WARN_ON(!local->resuming))
  997. return;
  998. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  999. return;
  1000. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1001. mutex_lock(&local->key_mtx);
  1002. list_for_each_entry(key, &sdata->key_list, list)
  1003. key->flags |= KEY_FLAG_TAINTED;
  1004. mutex_unlock(&local->key_mtx);
  1005. }
  1006. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1007. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1008. enum ieee80211_smps_mode *smps_mode)
  1009. {
  1010. if (ifmgd->associated) {
  1011. *smps_mode = ifmgd->ap_smps;
  1012. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1013. if (ifmgd->powersave)
  1014. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1015. else
  1016. *smps_mode = IEEE80211_SMPS_OFF;
  1017. }
  1018. return 1;
  1019. }
  1020. return 0;
  1021. }
  1022. /* must hold iflist_mtx */
  1023. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1024. {
  1025. struct ieee80211_sub_if_data *sdata;
  1026. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1027. int count = 0;
  1028. lockdep_assert_held(&local->iflist_mtx);
  1029. /*
  1030. * This function could be improved to handle multiple
  1031. * interfaces better, but right now it makes any
  1032. * non-station interfaces force SM PS to be turned
  1033. * off. If there are multiple station interfaces it
  1034. * could also use the best possible mode, e.g. if
  1035. * one is in static and the other in dynamic then
  1036. * dynamic is ok.
  1037. */
  1038. list_for_each_entry(sdata, &local->interfaces, list) {
  1039. if (!ieee80211_sdata_running(sdata))
  1040. continue;
  1041. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1042. goto set;
  1043. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1044. if (count > 1) {
  1045. smps_mode = IEEE80211_SMPS_OFF;
  1046. break;
  1047. }
  1048. }
  1049. if (smps_mode == local->smps_mode)
  1050. return;
  1051. set:
  1052. local->smps_mode = smps_mode;
  1053. /* changed flag is auto-detected for this */
  1054. ieee80211_hw_config(local, 0);
  1055. }
  1056. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1057. {
  1058. int i;
  1059. for (i = 0; i < n_ids; i++)
  1060. if (ids[i] == id)
  1061. return true;
  1062. return false;
  1063. }
  1064. /**
  1065. * ieee80211_ie_split - split an IE buffer according to ordering
  1066. *
  1067. * @ies: the IE buffer
  1068. * @ielen: the length of the IE buffer
  1069. * @ids: an array with element IDs that are allowed before
  1070. * the split
  1071. * @n_ids: the size of the element ID array
  1072. * @offset: offset where to start splitting in the buffer
  1073. *
  1074. * This function splits an IE buffer by updating the @offset
  1075. * variable to point to the location where the buffer should be
  1076. * split.
  1077. *
  1078. * It assumes that the given IE buffer is well-formed, this
  1079. * has to be guaranteed by the caller!
  1080. *
  1081. * It also assumes that the IEs in the buffer are ordered
  1082. * correctly, if not the result of using this function will not
  1083. * be ordered correctly either, i.e. it does no reordering.
  1084. *
  1085. * The function returns the offset where the next part of the
  1086. * buffer starts, which may be @ielen if the entire (remainder)
  1087. * of the buffer should be used.
  1088. */
  1089. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1090. const u8 *ids, int n_ids, size_t offset)
  1091. {
  1092. size_t pos = offset;
  1093. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1094. pos += 2 + ies[pos + 1];
  1095. return pos;
  1096. }
  1097. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1098. {
  1099. size_t pos = offset;
  1100. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1101. pos += 2 + ies[pos + 1];
  1102. return pos;
  1103. }
  1104. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1105. int rssi_min_thold,
  1106. int rssi_max_thold)
  1107. {
  1108. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1109. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1110. return;
  1111. /*
  1112. * Scale up threshold values before storing it, as the RSSI averaging
  1113. * algorithm uses a scaled up value as well. Change this scaling
  1114. * factor if the RSSI averaging algorithm changes.
  1115. */
  1116. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1117. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1118. }
  1119. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1120. int rssi_min_thold,
  1121. int rssi_max_thold)
  1122. {
  1123. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1124. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1125. rssi_min_thold > rssi_max_thold);
  1126. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1127. rssi_max_thold);
  1128. }
  1129. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1130. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1131. {
  1132. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1133. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1134. }
  1135. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1136. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_supported_band *sband,
  1137. u16 cap)
  1138. {
  1139. __le16 tmp;
  1140. *pos++ = WLAN_EID_HT_CAPABILITY;
  1141. *pos++ = sizeof(struct ieee80211_ht_cap);
  1142. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1143. /* capability flags */
  1144. tmp = cpu_to_le16(cap);
  1145. memcpy(pos, &tmp, sizeof(u16));
  1146. pos += sizeof(u16);
  1147. /* AMPDU parameters */
  1148. *pos++ = sband->ht_cap.ampdu_factor |
  1149. (sband->ht_cap.ampdu_density <<
  1150. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1151. /* MCS set */
  1152. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  1153. pos += sizeof(sband->ht_cap.mcs);
  1154. /* extended capabilities */
  1155. pos += sizeof(__le16);
  1156. /* BF capabilities */
  1157. pos += sizeof(__le32);
  1158. /* antenna selection */
  1159. pos += sizeof(u8);
  1160. return pos;
  1161. }
  1162. u8 *ieee80211_ie_build_ht_info(u8 *pos,
  1163. struct ieee80211_sta_ht_cap *ht_cap,
  1164. struct ieee80211_channel *channel,
  1165. enum nl80211_channel_type channel_type)
  1166. {
  1167. struct ieee80211_ht_info *ht_info;
  1168. /* Build HT Information */
  1169. *pos++ = WLAN_EID_HT_INFORMATION;
  1170. *pos++ = sizeof(struct ieee80211_ht_info);
  1171. ht_info = (struct ieee80211_ht_info *)pos;
  1172. ht_info->control_chan =
  1173. ieee80211_frequency_to_channel(channel->center_freq);
  1174. switch (channel_type) {
  1175. case NL80211_CHAN_HT40MINUS:
  1176. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1177. break;
  1178. case NL80211_CHAN_HT40PLUS:
  1179. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1180. break;
  1181. case NL80211_CHAN_HT20:
  1182. default:
  1183. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1184. break;
  1185. }
  1186. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
  1187. ht_info->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1188. ht_info->operation_mode = 0x0000;
  1189. ht_info->stbc_param = 0x0000;
  1190. /* It seems that Basic MCS set and Supported MCS set
  1191. are identical for the first 10 bytes */
  1192. memset(&ht_info->basic_set, 0, 16);
  1193. memcpy(&ht_info->basic_set, &ht_cap->mcs, 10);
  1194. return pos + sizeof(struct ieee80211_ht_info);
  1195. }
  1196. enum nl80211_channel_type
  1197. ieee80211_ht_info_to_channel_type(struct ieee80211_ht_info *ht_info)
  1198. {
  1199. enum nl80211_channel_type channel_type;
  1200. if (!ht_info)
  1201. return NL80211_CHAN_NO_HT;
  1202. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1203. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1204. channel_type = NL80211_CHAN_HT20;
  1205. break;
  1206. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1207. channel_type = NL80211_CHAN_HT40PLUS;
  1208. break;
  1209. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1210. channel_type = NL80211_CHAN_HT40MINUS;
  1211. break;
  1212. default:
  1213. channel_type = NL80211_CHAN_NO_HT;
  1214. }
  1215. return channel_type;
  1216. }
  1217. int ieee80211_add_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1218. {
  1219. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1220. struct ieee80211_local *local = sdata->local;
  1221. struct ieee80211_supported_band *sband;
  1222. int rate;
  1223. u8 i, rates, *pos;
  1224. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1225. rates = sband->n_bitrates;
  1226. if (rates > 8)
  1227. rates = 8;
  1228. if (skb_tailroom(skb) < rates + 2)
  1229. return -ENOMEM;
  1230. pos = skb_put(skb, rates + 2);
  1231. *pos++ = WLAN_EID_SUPP_RATES;
  1232. *pos++ = rates;
  1233. for (i = 0; i < rates; i++) {
  1234. rate = sband->bitrates[i].bitrate;
  1235. *pos++ = (u8) (rate / 5);
  1236. }
  1237. return 0;
  1238. }
  1239. int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1240. {
  1241. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1242. struct ieee80211_local *local = sdata->local;
  1243. struct ieee80211_supported_band *sband;
  1244. int rate;
  1245. u8 i, exrates, *pos;
  1246. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1247. exrates = sband->n_bitrates;
  1248. if (exrates > 8)
  1249. exrates -= 8;
  1250. else
  1251. exrates = 0;
  1252. if (skb_tailroom(skb) < exrates + 2)
  1253. return -ENOMEM;
  1254. if (exrates) {
  1255. pos = skb_put(skb, exrates + 2);
  1256. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1257. *pos++ = exrates;
  1258. for (i = 8; i < sband->n_bitrates; i++) {
  1259. rate = sband->bitrates[i].bitrate;
  1260. *pos++ = (u8) (rate / 5);
  1261. }
  1262. }
  1263. return 0;
  1264. }