util.c 42 KB

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