util.c 35 KB

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