util.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. int 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, ret = 0, 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. ret++;
  330. queue = skb_get_queue_mapping(skb);
  331. __skb_queue_tail(&local->pending[queue], skb);
  332. }
  333. if (fn)
  334. fn(data);
  335. for (i = 0; i < hw->queues; i++)
  336. __ieee80211_wake_queue(hw, i,
  337. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  338. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  339. return ret;
  340. }
  341. int ieee80211_add_pending_skbs(struct ieee80211_local *local,
  342. struct sk_buff_head *skbs)
  343. {
  344. return 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. void ieee802_11_parse_elems(u8 *start, size_t len,
  473. struct ieee802_11_elems *elems)
  474. {
  475. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  476. }
  477. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  478. {
  479. struct ieee80211_local *local = sdata->local;
  480. struct ieee80211_tx_queue_params qparam;
  481. int queue;
  482. bool use_11b;
  483. int aCWmin, aCWmax;
  484. if (!local->ops->conf_tx)
  485. return;
  486. memset(&qparam, 0, sizeof(qparam));
  487. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  488. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  489. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  490. /* Set defaults according to 802.11-2007 Table 7-37 */
  491. aCWmax = 1023;
  492. if (use_11b)
  493. aCWmin = 31;
  494. else
  495. aCWmin = 15;
  496. switch (queue) {
  497. case 3: /* AC_BK */
  498. qparam.cw_max = aCWmax;
  499. qparam.cw_min = aCWmin;
  500. qparam.txop = 0;
  501. qparam.aifs = 7;
  502. break;
  503. default: /* never happens but let's not leave undefined */
  504. case 2: /* AC_BE */
  505. qparam.cw_max = aCWmax;
  506. qparam.cw_min = aCWmin;
  507. qparam.txop = 0;
  508. qparam.aifs = 3;
  509. break;
  510. case 1: /* AC_VI */
  511. qparam.cw_max = aCWmin;
  512. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  513. if (use_11b)
  514. qparam.txop = 6016/32;
  515. else
  516. qparam.txop = 3008/32;
  517. qparam.aifs = 2;
  518. break;
  519. case 0: /* AC_VO */
  520. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  521. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  522. if (use_11b)
  523. qparam.txop = 3264/32;
  524. else
  525. qparam.txop = 1504/32;
  526. qparam.aifs = 2;
  527. break;
  528. }
  529. qparam.uapsd = false;
  530. local->tx_conf[queue] = qparam;
  531. drv_conf_tx(local, queue, &qparam);
  532. }
  533. /* after reinitialize QoS TX queues setting to default,
  534. * disable QoS at all */
  535. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  536. sdata->vif.bss_conf.qos =
  537. sdata->vif.type != NL80211_IFTYPE_STATION;
  538. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  539. }
  540. }
  541. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  542. const size_t supp_rates_len,
  543. const u8 *supp_rates)
  544. {
  545. struct ieee80211_local *local = sdata->local;
  546. int i, have_higher_than_11mbit = 0;
  547. /* cf. IEEE 802.11 9.2.12 */
  548. for (i = 0; i < supp_rates_len; i++)
  549. if ((supp_rates[i] & 0x7f) * 5 > 110)
  550. have_higher_than_11mbit = 1;
  551. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  552. have_higher_than_11mbit)
  553. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  554. else
  555. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  556. ieee80211_set_wmm_default(sdata);
  557. }
  558. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  559. enum ieee80211_band band)
  560. {
  561. struct ieee80211_supported_band *sband;
  562. struct ieee80211_rate *bitrates;
  563. u32 mandatory_rates;
  564. enum ieee80211_rate_flags mandatory_flag;
  565. int i;
  566. sband = local->hw.wiphy->bands[band];
  567. if (!sband) {
  568. WARN_ON(1);
  569. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  570. }
  571. if (band == IEEE80211_BAND_2GHZ)
  572. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  573. else
  574. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  575. bitrates = sband->bitrates;
  576. mandatory_rates = 0;
  577. for (i = 0; i < sband->n_bitrates; i++)
  578. if (bitrates[i].flags & mandatory_flag)
  579. mandatory_rates |= BIT(i);
  580. return mandatory_rates;
  581. }
  582. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  583. u16 transaction, u16 auth_alg,
  584. u8 *extra, size_t extra_len, const u8 *bssid,
  585. const u8 *key, u8 key_len, u8 key_idx)
  586. {
  587. struct ieee80211_local *local = sdata->local;
  588. struct sk_buff *skb;
  589. struct ieee80211_mgmt *mgmt;
  590. int err;
  591. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  592. sizeof(*mgmt) + 6 + extra_len);
  593. if (!skb)
  594. return;
  595. skb_reserve(skb, local->hw.extra_tx_headroom);
  596. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  597. memset(mgmt, 0, 24 + 6);
  598. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  599. IEEE80211_STYPE_AUTH);
  600. memcpy(mgmt->da, bssid, ETH_ALEN);
  601. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  602. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  603. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  604. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  605. mgmt->u.auth.status_code = cpu_to_le16(0);
  606. if (extra)
  607. memcpy(skb_put(skb, extra_len), extra, extra_len);
  608. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  609. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  610. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  611. WARN_ON(err);
  612. }
  613. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  614. ieee80211_tx_skb(sdata, skb);
  615. }
  616. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  617. const u8 *ie, size_t ie_len,
  618. enum ieee80211_band band, u32 rate_mask,
  619. u8 channel)
  620. {
  621. struct ieee80211_supported_band *sband;
  622. u8 *pos;
  623. size_t offset = 0, noffset;
  624. int supp_rates_len, i;
  625. u8 rates[32];
  626. int num_rates;
  627. int ext_rates_len;
  628. sband = local->hw.wiphy->bands[band];
  629. pos = buffer;
  630. num_rates = 0;
  631. for (i = 0; i < sband->n_bitrates; i++) {
  632. if ((BIT(i) & rate_mask) == 0)
  633. continue; /* skip rate */
  634. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  635. }
  636. supp_rates_len = min_t(int, num_rates, 8);
  637. *pos++ = WLAN_EID_SUPP_RATES;
  638. *pos++ = supp_rates_len;
  639. memcpy(pos, rates, supp_rates_len);
  640. pos += supp_rates_len;
  641. /* insert "request information" if in custom IEs */
  642. if (ie && ie_len) {
  643. static const u8 before_extrates[] = {
  644. WLAN_EID_SSID,
  645. WLAN_EID_SUPP_RATES,
  646. WLAN_EID_REQUEST,
  647. };
  648. noffset = ieee80211_ie_split(ie, ie_len,
  649. before_extrates,
  650. ARRAY_SIZE(before_extrates),
  651. offset);
  652. memcpy(pos, ie + offset, noffset - offset);
  653. pos += noffset - offset;
  654. offset = noffset;
  655. }
  656. ext_rates_len = num_rates - supp_rates_len;
  657. if (ext_rates_len > 0) {
  658. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  659. *pos++ = ext_rates_len;
  660. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  661. pos += ext_rates_len;
  662. }
  663. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  664. *pos++ = WLAN_EID_DS_PARAMS;
  665. *pos++ = 1;
  666. *pos++ = channel;
  667. }
  668. /* insert custom IEs that go before HT */
  669. if (ie && ie_len) {
  670. static const u8 before_ht[] = {
  671. WLAN_EID_SSID,
  672. WLAN_EID_SUPP_RATES,
  673. WLAN_EID_REQUEST,
  674. WLAN_EID_EXT_SUPP_RATES,
  675. WLAN_EID_DS_PARAMS,
  676. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  677. };
  678. noffset = ieee80211_ie_split(ie, ie_len,
  679. before_ht, ARRAY_SIZE(before_ht),
  680. offset);
  681. memcpy(pos, ie + offset, noffset - offset);
  682. pos += noffset - offset;
  683. offset = noffset;
  684. }
  685. if (sband->ht_cap.ht_supported) {
  686. u16 cap = sband->ht_cap.cap;
  687. __le16 tmp;
  688. *pos++ = WLAN_EID_HT_CAPABILITY;
  689. *pos++ = sizeof(struct ieee80211_ht_cap);
  690. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  691. tmp = cpu_to_le16(cap);
  692. memcpy(pos, &tmp, sizeof(u16));
  693. pos += sizeof(u16);
  694. *pos++ = sband->ht_cap.ampdu_factor |
  695. (sband->ht_cap.ampdu_density <<
  696. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  697. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  698. pos += sizeof(sband->ht_cap.mcs);
  699. pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
  700. }
  701. /*
  702. * If adding more here, adjust code in main.c
  703. * that calculates local->scan_ies_len.
  704. */
  705. /* add any remaining custom IEs */
  706. if (ie && ie_len) {
  707. noffset = ie_len;
  708. memcpy(pos, ie + offset, noffset - offset);
  709. pos += noffset - offset;
  710. }
  711. return pos - buffer;
  712. }
  713. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  714. u8 *dst, u32 ratemask,
  715. const u8 *ssid, size_t ssid_len,
  716. const u8 *ie, size_t ie_len,
  717. bool directed)
  718. {
  719. struct ieee80211_local *local = sdata->local;
  720. struct sk_buff *skb;
  721. struct ieee80211_mgmt *mgmt;
  722. size_t buf_len;
  723. u8 *buf;
  724. u8 chan;
  725. /* FIXME: come up with a proper value */
  726. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  727. if (!buf)
  728. return NULL;
  729. /*
  730. * Do not send DS Channel parameter for directed probe requests
  731. * in order to maximize the chance that we get a response. Some
  732. * badly-behaved APs don't respond when this parameter is included.
  733. */
  734. if (directed)
  735. chan = 0;
  736. else
  737. chan = ieee80211_frequency_to_channel(
  738. local->hw.conf.channel->center_freq);
  739. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  740. local->hw.conf.channel->band,
  741. ratemask, chan);
  742. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  743. ssid, ssid_len,
  744. buf, buf_len);
  745. if (dst) {
  746. mgmt = (struct ieee80211_mgmt *) skb->data;
  747. memcpy(mgmt->da, dst, ETH_ALEN);
  748. memcpy(mgmt->bssid, dst, ETH_ALEN);
  749. }
  750. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  751. kfree(buf);
  752. return skb;
  753. }
  754. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  755. const u8 *ssid, size_t ssid_len,
  756. const u8 *ie, size_t ie_len,
  757. u32 ratemask, bool directed)
  758. {
  759. struct sk_buff *skb;
  760. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  761. ie, ie_len, directed);
  762. if (skb)
  763. ieee80211_tx_skb(sdata, skb);
  764. }
  765. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  766. struct ieee802_11_elems *elems,
  767. enum ieee80211_band band)
  768. {
  769. struct ieee80211_supported_band *sband;
  770. struct ieee80211_rate *bitrates;
  771. size_t num_rates;
  772. u32 supp_rates;
  773. int i, j;
  774. sband = local->hw.wiphy->bands[band];
  775. if (!sband) {
  776. WARN_ON(1);
  777. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  778. }
  779. bitrates = sband->bitrates;
  780. num_rates = sband->n_bitrates;
  781. supp_rates = 0;
  782. for (i = 0; i < elems->supp_rates_len +
  783. elems->ext_supp_rates_len; i++) {
  784. u8 rate = 0;
  785. int own_rate;
  786. if (i < elems->supp_rates_len)
  787. rate = elems->supp_rates[i];
  788. else if (elems->ext_supp_rates)
  789. rate = elems->ext_supp_rates
  790. [i - elems->supp_rates_len];
  791. own_rate = 5 * (rate & 0x7f);
  792. for (j = 0; j < num_rates; j++)
  793. if (bitrates[j].bitrate == own_rate)
  794. supp_rates |= BIT(j);
  795. }
  796. return supp_rates;
  797. }
  798. void ieee80211_stop_device(struct ieee80211_local *local)
  799. {
  800. ieee80211_led_radio(local, false);
  801. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  802. cancel_work_sync(&local->reconfig_filter);
  803. flush_workqueue(local->workqueue);
  804. drv_stop(local);
  805. }
  806. int ieee80211_reconfig(struct ieee80211_local *local)
  807. {
  808. struct ieee80211_hw *hw = &local->hw;
  809. struct ieee80211_sub_if_data *sdata;
  810. struct sta_info *sta;
  811. int res, i;
  812. #ifdef CONFIG_PM
  813. if (local->suspended)
  814. local->resuming = true;
  815. if (local->wowlan) {
  816. local->wowlan = false;
  817. res = drv_resume(local);
  818. if (res < 0) {
  819. local->resuming = false;
  820. return res;
  821. }
  822. if (res == 0)
  823. goto wake_up;
  824. WARN_ON(res > 1);
  825. /*
  826. * res is 1, which means the driver requested
  827. * to go through a regular reset on wakeup.
  828. */
  829. }
  830. #endif
  831. /* setup fragmentation threshold */
  832. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  833. /* setup RTS threshold */
  834. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  835. /* reset coverage class */
  836. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  837. /* everything else happens only if HW was up & running */
  838. if (!local->open_count)
  839. goto wake_up;
  840. /*
  841. * Upon resume hardware can sometimes be goofy due to
  842. * various platform / driver / bus issues, so restarting
  843. * the device may at times not work immediately. Propagate
  844. * the error.
  845. */
  846. res = drv_start(local);
  847. if (res) {
  848. WARN(local->suspended, "Hardware became unavailable "
  849. "upon resume. This could be a software issue "
  850. "prior to suspend or a hardware issue.\n");
  851. return res;
  852. }
  853. ieee80211_led_radio(local, true);
  854. ieee80211_mod_tpt_led_trig(local,
  855. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  856. /* add interfaces */
  857. list_for_each_entry(sdata, &local->interfaces, list) {
  858. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  859. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  860. ieee80211_sdata_running(sdata))
  861. res = drv_add_interface(local, &sdata->vif);
  862. }
  863. /* add STAs back */
  864. mutex_lock(&local->sta_mtx);
  865. list_for_each_entry(sta, &local->sta_list, list) {
  866. if (sta->uploaded) {
  867. sdata = sta->sdata;
  868. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  869. sdata = container_of(sdata->bss,
  870. struct ieee80211_sub_if_data,
  871. u.ap);
  872. memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
  873. WARN_ON(drv_sta_add(local, sdata, &sta->sta));
  874. }
  875. }
  876. mutex_unlock(&local->sta_mtx);
  877. /* reconfigure tx conf */
  878. for (i = 0; i < hw->queues; i++)
  879. drv_conf_tx(local, i, &local->tx_conf[i]);
  880. /* reconfigure hardware */
  881. ieee80211_hw_config(local, ~0);
  882. ieee80211_configure_filter(local);
  883. /* Finally also reconfigure all the BSS information */
  884. list_for_each_entry(sdata, &local->interfaces, list) {
  885. u32 changed;
  886. if (!ieee80211_sdata_running(sdata))
  887. continue;
  888. /* common change flags for all interface types */
  889. changed = BSS_CHANGED_ERP_CTS_PROT |
  890. BSS_CHANGED_ERP_PREAMBLE |
  891. BSS_CHANGED_ERP_SLOT |
  892. BSS_CHANGED_HT |
  893. BSS_CHANGED_BASIC_RATES |
  894. BSS_CHANGED_BEACON_INT |
  895. BSS_CHANGED_BSSID |
  896. BSS_CHANGED_CQM |
  897. BSS_CHANGED_QOS;
  898. switch (sdata->vif.type) {
  899. case NL80211_IFTYPE_STATION:
  900. changed |= BSS_CHANGED_ASSOC;
  901. mutex_lock(&sdata->u.mgd.mtx);
  902. ieee80211_bss_info_change_notify(sdata, changed);
  903. mutex_unlock(&sdata->u.mgd.mtx);
  904. break;
  905. case NL80211_IFTYPE_ADHOC:
  906. changed |= BSS_CHANGED_IBSS;
  907. /* fall through */
  908. case NL80211_IFTYPE_AP:
  909. changed |= BSS_CHANGED_SSID;
  910. /* fall through */
  911. case NL80211_IFTYPE_MESH_POINT:
  912. changed |= BSS_CHANGED_BEACON |
  913. BSS_CHANGED_BEACON_ENABLED;
  914. ieee80211_bss_info_change_notify(sdata, changed);
  915. break;
  916. case NL80211_IFTYPE_WDS:
  917. break;
  918. case NL80211_IFTYPE_AP_VLAN:
  919. case NL80211_IFTYPE_MONITOR:
  920. /* ignore virtual */
  921. break;
  922. case NL80211_IFTYPE_UNSPECIFIED:
  923. case NUM_NL80211_IFTYPES:
  924. case NL80211_IFTYPE_P2P_CLIENT:
  925. case NL80211_IFTYPE_P2P_GO:
  926. WARN_ON(1);
  927. break;
  928. }
  929. }
  930. /*
  931. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  932. * sessions can be established after a resume.
  933. *
  934. * Also tear down aggregation sessions since reconfiguring
  935. * them in a hardware restart scenario is not easily done
  936. * right now, and the hardware will have lost information
  937. * about the sessions, but we and the AP still think they
  938. * are active. This is really a workaround though.
  939. */
  940. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  941. mutex_lock(&local->sta_mtx);
  942. list_for_each_entry(sta, &local->sta_list, list) {
  943. ieee80211_sta_tear_down_BA_sessions(sta, true);
  944. clear_sta_flags(sta, WLAN_STA_BLOCK_BA);
  945. }
  946. mutex_unlock(&local->sta_mtx);
  947. }
  948. /* add back keys */
  949. list_for_each_entry(sdata, &local->interfaces, list)
  950. if (ieee80211_sdata_running(sdata))
  951. ieee80211_enable_keys(sdata);
  952. wake_up:
  953. ieee80211_wake_queues_by_reason(hw,
  954. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  955. /*
  956. * If this is for hw restart things are still running.
  957. * We may want to change that later, however.
  958. */
  959. if (!local->suspended)
  960. return 0;
  961. #ifdef CONFIG_PM
  962. /* first set suspended false, then resuming */
  963. local->suspended = false;
  964. mb();
  965. local->resuming = false;
  966. list_for_each_entry(sdata, &local->interfaces, list) {
  967. switch(sdata->vif.type) {
  968. case NL80211_IFTYPE_STATION:
  969. ieee80211_sta_restart(sdata);
  970. break;
  971. case NL80211_IFTYPE_ADHOC:
  972. ieee80211_ibss_restart(sdata);
  973. break;
  974. case NL80211_IFTYPE_MESH_POINT:
  975. ieee80211_mesh_restart(sdata);
  976. break;
  977. default:
  978. break;
  979. }
  980. }
  981. mod_timer(&local->sta_cleanup, jiffies + 1);
  982. mutex_lock(&local->sta_mtx);
  983. list_for_each_entry(sta, &local->sta_list, list)
  984. mesh_plink_restart(sta);
  985. mutex_unlock(&local->sta_mtx);
  986. #else
  987. WARN_ON(1);
  988. #endif
  989. return 0;
  990. }
  991. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  992. {
  993. struct ieee80211_sub_if_data *sdata;
  994. struct ieee80211_local *local;
  995. struct ieee80211_key *key;
  996. if (WARN_ON(!vif))
  997. return;
  998. sdata = vif_to_sdata(vif);
  999. local = sdata->local;
  1000. if (WARN_ON(!local->resuming))
  1001. return;
  1002. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1003. return;
  1004. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1005. mutex_lock(&local->key_mtx);
  1006. list_for_each_entry(key, &sdata->key_list, list)
  1007. key->flags |= KEY_FLAG_TAINTED;
  1008. mutex_unlock(&local->key_mtx);
  1009. }
  1010. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1011. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1012. enum ieee80211_smps_mode *smps_mode)
  1013. {
  1014. if (ifmgd->associated) {
  1015. *smps_mode = ifmgd->ap_smps;
  1016. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1017. if (ifmgd->powersave)
  1018. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1019. else
  1020. *smps_mode = IEEE80211_SMPS_OFF;
  1021. }
  1022. return 1;
  1023. }
  1024. return 0;
  1025. }
  1026. /* must hold iflist_mtx */
  1027. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1028. {
  1029. struct ieee80211_sub_if_data *sdata;
  1030. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1031. int count = 0;
  1032. lockdep_assert_held(&local->iflist_mtx);
  1033. /*
  1034. * This function could be improved to handle multiple
  1035. * interfaces better, but right now it makes any
  1036. * non-station interfaces force SM PS to be turned
  1037. * off. If there are multiple station interfaces it
  1038. * could also use the best possible mode, e.g. if
  1039. * one is in static and the other in dynamic then
  1040. * dynamic is ok.
  1041. */
  1042. list_for_each_entry(sdata, &local->interfaces, list) {
  1043. if (!ieee80211_sdata_running(sdata))
  1044. continue;
  1045. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1046. goto set;
  1047. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1048. if (count > 1) {
  1049. smps_mode = IEEE80211_SMPS_OFF;
  1050. break;
  1051. }
  1052. }
  1053. if (smps_mode == local->smps_mode)
  1054. return;
  1055. set:
  1056. local->smps_mode = smps_mode;
  1057. /* changed flag is auto-detected for this */
  1058. ieee80211_hw_config(local, 0);
  1059. }
  1060. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1061. {
  1062. int i;
  1063. for (i = 0; i < n_ids; i++)
  1064. if (ids[i] == id)
  1065. return true;
  1066. return false;
  1067. }
  1068. /**
  1069. * ieee80211_ie_split - split an IE buffer according to ordering
  1070. *
  1071. * @ies: the IE buffer
  1072. * @ielen: the length of the IE buffer
  1073. * @ids: an array with element IDs that are allowed before
  1074. * the split
  1075. * @n_ids: the size of the element ID array
  1076. * @offset: offset where to start splitting in the buffer
  1077. *
  1078. * This function splits an IE buffer by updating the @offset
  1079. * variable to point to the location where the buffer should be
  1080. * split.
  1081. *
  1082. * It assumes that the given IE buffer is well-formed, this
  1083. * has to be guaranteed by the caller!
  1084. *
  1085. * It also assumes that the IEs in the buffer are ordered
  1086. * correctly, if not the result of using this function will not
  1087. * be ordered correctly either, i.e. it does no reordering.
  1088. *
  1089. * The function returns the offset where the next part of the
  1090. * buffer starts, which may be @ielen if the entire (remainder)
  1091. * of the buffer should be used.
  1092. */
  1093. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1094. const u8 *ids, int n_ids, size_t offset)
  1095. {
  1096. size_t pos = offset;
  1097. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1098. pos += 2 + ies[pos + 1];
  1099. return pos;
  1100. }
  1101. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1102. {
  1103. size_t pos = offset;
  1104. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1105. pos += 2 + ies[pos + 1];
  1106. return pos;
  1107. }
  1108. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1109. int rssi_min_thold,
  1110. int rssi_max_thold)
  1111. {
  1112. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1113. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1114. return;
  1115. /*
  1116. * Scale up threshold values before storing it, as the RSSI averaging
  1117. * algorithm uses a scaled up value as well. Change this scaling
  1118. * factor if the RSSI averaging algorithm changes.
  1119. */
  1120. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1121. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1122. }
  1123. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1124. int rssi_min_thold,
  1125. int rssi_max_thold)
  1126. {
  1127. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1128. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1129. rssi_min_thold > rssi_max_thold);
  1130. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1131. rssi_max_thold);
  1132. }
  1133. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1134. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1135. {
  1136. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1137. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1138. }
  1139. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);