util.c 35 KB

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