util.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  238. return;
  239. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  240. if (local->queue_stop_reasons[queue] != 0)
  241. /* someone still has this queue stopped */
  242. return;
  243. if (skb_queue_empty(&local->pending[queue])) {
  244. rcu_read_lock();
  245. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  246. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  247. continue;
  248. netif_wake_subqueue(sdata->dev, queue);
  249. }
  250. rcu_read_unlock();
  251. } else
  252. tasklet_schedule(&local->tx_pending_tasklet);
  253. }
  254. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  255. enum queue_stop_reason reason)
  256. {
  257. struct ieee80211_local *local = hw_to_local(hw);
  258. unsigned long flags;
  259. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  260. __ieee80211_wake_queue(hw, queue, reason);
  261. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  262. }
  263. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  264. {
  265. ieee80211_wake_queue_by_reason(hw, queue,
  266. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  267. }
  268. EXPORT_SYMBOL(ieee80211_wake_queue);
  269. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  270. enum queue_stop_reason reason)
  271. {
  272. struct ieee80211_local *local = hw_to_local(hw);
  273. struct ieee80211_sub_if_data *sdata;
  274. trace_stop_queue(local, queue, reason);
  275. if (WARN_ON(queue >= hw->queues))
  276. return;
  277. if (test_bit(reason, &local->queue_stop_reasons[queue]))
  278. return;
  279. __set_bit(reason, &local->queue_stop_reasons[queue]);
  280. rcu_read_lock();
  281. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  282. netif_stop_subqueue(sdata->dev, queue);
  283. rcu_read_unlock();
  284. }
  285. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  286. enum queue_stop_reason reason)
  287. {
  288. struct ieee80211_local *local = hw_to_local(hw);
  289. unsigned long flags;
  290. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  291. __ieee80211_stop_queue(hw, queue, reason);
  292. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  293. }
  294. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  295. {
  296. ieee80211_stop_queue_by_reason(hw, queue,
  297. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  298. }
  299. EXPORT_SYMBOL(ieee80211_stop_queue);
  300. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  301. struct sk_buff *skb)
  302. {
  303. struct ieee80211_hw *hw = &local->hw;
  304. unsigned long flags;
  305. int queue = skb_get_queue_mapping(skb);
  306. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  307. if (WARN_ON(!info->control.vif)) {
  308. kfree_skb(skb);
  309. return;
  310. }
  311. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  312. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  313. __skb_queue_tail(&local->pending[queue], skb);
  314. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  315. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  316. }
  317. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  318. struct sk_buff_head *skbs,
  319. void (*fn)(void *data), void *data)
  320. {
  321. struct ieee80211_hw *hw = &local->hw;
  322. struct sk_buff *skb;
  323. unsigned long flags;
  324. int queue, i;
  325. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  326. while ((skb = skb_dequeue(skbs))) {
  327. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  328. if (WARN_ON(!info->control.vif)) {
  329. kfree_skb(skb);
  330. continue;
  331. }
  332. queue = skb_get_queue_mapping(skb);
  333. __ieee80211_stop_queue(hw, queue,
  334. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  335. __skb_queue_tail(&local->pending[queue], skb);
  336. }
  337. if (fn)
  338. fn(data);
  339. for (i = 0; i < hw->queues; i++)
  340. __ieee80211_wake_queue(hw, i,
  341. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  342. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  343. }
  344. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  345. enum queue_stop_reason reason)
  346. {
  347. struct ieee80211_local *local = hw_to_local(hw);
  348. unsigned long flags;
  349. int i;
  350. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  351. for (i = 0; i < hw->queues; i++)
  352. __ieee80211_stop_queue(hw, i, reason);
  353. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  354. }
  355. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  356. {
  357. ieee80211_stop_queues_by_reason(hw,
  358. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  359. }
  360. EXPORT_SYMBOL(ieee80211_stop_queues);
  361. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  362. {
  363. struct ieee80211_local *local = hw_to_local(hw);
  364. unsigned long flags;
  365. int ret;
  366. if (WARN_ON(queue >= hw->queues))
  367. return true;
  368. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  369. ret = !!local->queue_stop_reasons[queue];
  370. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  371. return ret;
  372. }
  373. EXPORT_SYMBOL(ieee80211_queue_stopped);
  374. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  375. enum queue_stop_reason reason)
  376. {
  377. struct ieee80211_local *local = hw_to_local(hw);
  378. unsigned long flags;
  379. int i;
  380. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  381. for (i = 0; i < hw->queues; i++)
  382. __ieee80211_wake_queue(hw, i, reason);
  383. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  384. }
  385. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  386. {
  387. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  388. }
  389. EXPORT_SYMBOL(ieee80211_wake_queues);
  390. void ieee80211_iterate_active_interfaces(
  391. struct ieee80211_hw *hw,
  392. void (*iterator)(void *data, u8 *mac,
  393. struct ieee80211_vif *vif),
  394. void *data)
  395. {
  396. struct ieee80211_local *local = hw_to_local(hw);
  397. struct ieee80211_sub_if_data *sdata;
  398. mutex_lock(&local->iflist_mtx);
  399. list_for_each_entry(sdata, &local->interfaces, list) {
  400. switch (sdata->vif.type) {
  401. case NL80211_IFTYPE_MONITOR:
  402. case NL80211_IFTYPE_AP_VLAN:
  403. continue;
  404. default:
  405. break;
  406. }
  407. if (ieee80211_sdata_running(sdata))
  408. iterator(data, sdata->vif.addr,
  409. &sdata->vif);
  410. }
  411. mutex_unlock(&local->iflist_mtx);
  412. }
  413. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  414. void ieee80211_iterate_active_interfaces_atomic(
  415. struct ieee80211_hw *hw,
  416. void (*iterator)(void *data, u8 *mac,
  417. struct ieee80211_vif *vif),
  418. void *data)
  419. {
  420. struct ieee80211_local *local = hw_to_local(hw);
  421. struct ieee80211_sub_if_data *sdata;
  422. rcu_read_lock();
  423. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  424. switch (sdata->vif.type) {
  425. case NL80211_IFTYPE_MONITOR:
  426. case NL80211_IFTYPE_AP_VLAN:
  427. continue;
  428. default:
  429. break;
  430. }
  431. if (ieee80211_sdata_running(sdata))
  432. iterator(data, sdata->vif.addr,
  433. &sdata->vif);
  434. }
  435. rcu_read_unlock();
  436. }
  437. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  438. /*
  439. * Nothing should have been stuffed into the workqueue during
  440. * the suspend->resume cycle. If this WARN is seen then there
  441. * is a bug with either the driver suspend or something in
  442. * mac80211 stuffing into the workqueue which we haven't yet
  443. * cleared during mac80211's suspend cycle.
  444. */
  445. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  446. {
  447. if (WARN(local->suspended && !local->resuming,
  448. "queueing ieee80211 work while going to suspend\n"))
  449. return false;
  450. return true;
  451. }
  452. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  453. {
  454. struct ieee80211_local *local = hw_to_local(hw);
  455. if (!ieee80211_can_queue_work(local))
  456. return;
  457. queue_work(local->workqueue, work);
  458. }
  459. EXPORT_SYMBOL(ieee80211_queue_work);
  460. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  461. struct delayed_work *dwork,
  462. unsigned long delay)
  463. {
  464. struct ieee80211_local *local = hw_to_local(hw);
  465. if (!ieee80211_can_queue_work(local))
  466. return;
  467. queue_delayed_work(local->workqueue, dwork, delay);
  468. }
  469. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  470. 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. DECLARE_BITMAP(seen_elems, 256);
  478. bitmap_zero(seen_elems, 256);
  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. bool elem_parse_failed;
  485. id = *pos++;
  486. elen = *pos++;
  487. left -= 2;
  488. if (elen > left) {
  489. elems->parse_error = true;
  490. break;
  491. }
  492. if (id != WLAN_EID_VENDOR_SPECIFIC &&
  493. id != WLAN_EID_QUIET &&
  494. test_bit(id, seen_elems)) {
  495. elems->parse_error = true;
  496. left -= elen;
  497. pos += elen;
  498. continue;
  499. }
  500. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  501. crc = crc32_be(crc, pos - 2, elen + 2);
  502. elem_parse_failed = false;
  503. switch (id) {
  504. case WLAN_EID_SSID:
  505. elems->ssid = pos;
  506. elems->ssid_len = elen;
  507. break;
  508. case WLAN_EID_SUPP_RATES:
  509. elems->supp_rates = pos;
  510. elems->supp_rates_len = elen;
  511. break;
  512. case WLAN_EID_FH_PARAMS:
  513. elems->fh_params = pos;
  514. elems->fh_params_len = elen;
  515. break;
  516. case WLAN_EID_DS_PARAMS:
  517. elems->ds_params = pos;
  518. elems->ds_params_len = elen;
  519. break;
  520. case WLAN_EID_CF_PARAMS:
  521. elems->cf_params = pos;
  522. elems->cf_params_len = elen;
  523. break;
  524. case WLAN_EID_TIM:
  525. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  526. elems->tim = (void *)pos;
  527. elems->tim_len = elen;
  528. } else
  529. elem_parse_failed = true;
  530. break;
  531. case WLAN_EID_IBSS_PARAMS:
  532. elems->ibss_params = pos;
  533. elems->ibss_params_len = elen;
  534. break;
  535. case WLAN_EID_CHALLENGE:
  536. elems->challenge = pos;
  537. elems->challenge_len = elen;
  538. break;
  539. case WLAN_EID_VENDOR_SPECIFIC:
  540. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  541. pos[2] == 0xf2) {
  542. /* Microsoft OUI (00:50:F2) */
  543. if (calc_crc)
  544. crc = crc32_be(crc, pos - 2, elen + 2);
  545. if (pos[3] == 1) {
  546. /* OUI Type 1 - WPA IE */
  547. elems->wpa = pos;
  548. elems->wpa_len = elen;
  549. } else if (elen >= 5 && pos[3] == 2) {
  550. /* OUI Type 2 - WMM IE */
  551. if (pos[4] == 0) {
  552. elems->wmm_info = pos;
  553. elems->wmm_info_len = elen;
  554. } else if (pos[4] == 1) {
  555. elems->wmm_param = pos;
  556. elems->wmm_param_len = elen;
  557. }
  558. }
  559. }
  560. break;
  561. case WLAN_EID_RSN:
  562. elems->rsn = pos;
  563. elems->rsn_len = elen;
  564. break;
  565. case WLAN_EID_ERP_INFO:
  566. elems->erp_info = pos;
  567. elems->erp_info_len = elen;
  568. break;
  569. case WLAN_EID_EXT_SUPP_RATES:
  570. elems->ext_supp_rates = pos;
  571. elems->ext_supp_rates_len = elen;
  572. break;
  573. case WLAN_EID_HT_CAPABILITY:
  574. if (elen >= sizeof(struct ieee80211_ht_cap))
  575. elems->ht_cap_elem = (void *)pos;
  576. else
  577. elem_parse_failed = true;
  578. break;
  579. case WLAN_EID_HT_OPERATION:
  580. if (elen >= sizeof(struct ieee80211_ht_operation))
  581. elems->ht_operation = (void *)pos;
  582. else
  583. elem_parse_failed = true;
  584. break;
  585. case WLAN_EID_MESH_ID:
  586. elems->mesh_id = pos;
  587. elems->mesh_id_len = elen;
  588. break;
  589. case WLAN_EID_MESH_CONFIG:
  590. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  591. elems->mesh_config = (void *)pos;
  592. else
  593. elem_parse_failed = true;
  594. break;
  595. case WLAN_EID_PEER_MGMT:
  596. elems->peering = pos;
  597. elems->peering_len = elen;
  598. break;
  599. case WLAN_EID_PREQ:
  600. elems->preq = pos;
  601. elems->preq_len = elen;
  602. break;
  603. case WLAN_EID_PREP:
  604. elems->prep = pos;
  605. elems->prep_len = elen;
  606. break;
  607. case WLAN_EID_PERR:
  608. elems->perr = pos;
  609. elems->perr_len = elen;
  610. break;
  611. case WLAN_EID_RANN:
  612. if (elen >= sizeof(struct ieee80211_rann_ie))
  613. elems->rann = (void *)pos;
  614. else
  615. elem_parse_failed = true;
  616. break;
  617. case WLAN_EID_CHANNEL_SWITCH:
  618. elems->ch_switch_elem = pos;
  619. elems->ch_switch_elem_len = elen;
  620. break;
  621. case WLAN_EID_QUIET:
  622. if (!elems->quiet_elem) {
  623. elems->quiet_elem = pos;
  624. elems->quiet_elem_len = elen;
  625. }
  626. elems->num_of_quiet_elem++;
  627. break;
  628. case WLAN_EID_COUNTRY:
  629. elems->country_elem = pos;
  630. elems->country_elem_len = elen;
  631. break;
  632. case WLAN_EID_PWR_CONSTRAINT:
  633. elems->pwr_constr_elem = pos;
  634. elems->pwr_constr_elem_len = elen;
  635. break;
  636. case WLAN_EID_TIMEOUT_INTERVAL:
  637. elems->timeout_int = pos;
  638. elems->timeout_int_len = elen;
  639. break;
  640. default:
  641. break;
  642. }
  643. if (elem_parse_failed)
  644. elems->parse_error = true;
  645. else
  646. set_bit(id, seen_elems);
  647. left -= elen;
  648. pos += elen;
  649. }
  650. if (left != 0)
  651. elems->parse_error = true;
  652. return crc;
  653. }
  654. void ieee802_11_parse_elems(u8 *start, size_t len,
  655. struct ieee802_11_elems *elems)
  656. {
  657. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  658. }
  659. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  660. bool bss_notify)
  661. {
  662. struct ieee80211_local *local = sdata->local;
  663. struct ieee80211_tx_queue_params qparam;
  664. int ac;
  665. bool use_11b;
  666. int aCWmin, aCWmax;
  667. if (!local->ops->conf_tx)
  668. return;
  669. if (local->hw.queues < IEEE80211_NUM_ACS)
  670. return;
  671. memset(&qparam, 0, sizeof(qparam));
  672. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  673. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  674. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  675. /* Set defaults according to 802.11-2007 Table 7-37 */
  676. aCWmax = 1023;
  677. if (use_11b)
  678. aCWmin = 31;
  679. else
  680. aCWmin = 15;
  681. switch (ac) {
  682. case IEEE80211_AC_BK:
  683. qparam.cw_max = aCWmax;
  684. qparam.cw_min = aCWmin;
  685. qparam.txop = 0;
  686. qparam.aifs = 7;
  687. break;
  688. default: /* never happens but let's not leave undefined */
  689. case IEEE80211_AC_BE:
  690. qparam.cw_max = aCWmax;
  691. qparam.cw_min = aCWmin;
  692. qparam.txop = 0;
  693. qparam.aifs = 3;
  694. break;
  695. case IEEE80211_AC_VI:
  696. qparam.cw_max = aCWmin;
  697. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  698. if (use_11b)
  699. qparam.txop = 6016/32;
  700. else
  701. qparam.txop = 3008/32;
  702. qparam.aifs = 2;
  703. break;
  704. case IEEE80211_AC_VO:
  705. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  706. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  707. if (use_11b)
  708. qparam.txop = 3264/32;
  709. else
  710. qparam.txop = 1504/32;
  711. qparam.aifs = 2;
  712. break;
  713. }
  714. qparam.uapsd = false;
  715. sdata->tx_conf[ac] = qparam;
  716. drv_conf_tx(local, sdata, ac, &qparam);
  717. }
  718. /* after reinitialize QoS TX queues setting to default,
  719. * disable QoS at all */
  720. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  721. sdata->vif.bss_conf.qos =
  722. sdata->vif.type != NL80211_IFTYPE_STATION;
  723. if (bss_notify)
  724. ieee80211_bss_info_change_notify(sdata,
  725. BSS_CHANGED_QOS);
  726. }
  727. }
  728. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  729. const size_t supp_rates_len,
  730. const u8 *supp_rates)
  731. {
  732. struct ieee80211_local *local = sdata->local;
  733. int i, have_higher_than_11mbit = 0;
  734. /* cf. IEEE 802.11 9.2.12 */
  735. for (i = 0; i < supp_rates_len; i++)
  736. if ((supp_rates[i] & 0x7f) * 5 > 110)
  737. have_higher_than_11mbit = 1;
  738. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  739. have_higher_than_11mbit)
  740. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  741. else
  742. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  743. ieee80211_set_wmm_default(sdata, true);
  744. }
  745. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  746. enum ieee80211_band band)
  747. {
  748. struct ieee80211_supported_band *sband;
  749. struct ieee80211_rate *bitrates;
  750. u32 mandatory_rates;
  751. enum ieee80211_rate_flags mandatory_flag;
  752. int i;
  753. sband = local->hw.wiphy->bands[band];
  754. if (!sband) {
  755. WARN_ON(1);
  756. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  757. }
  758. if (band == IEEE80211_BAND_2GHZ)
  759. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  760. else
  761. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  762. bitrates = sband->bitrates;
  763. mandatory_rates = 0;
  764. for (i = 0; i < sband->n_bitrates; i++)
  765. if (bitrates[i].flags & mandatory_flag)
  766. mandatory_rates |= BIT(i);
  767. return mandatory_rates;
  768. }
  769. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  770. u16 transaction, u16 auth_alg,
  771. u8 *extra, size_t extra_len, const u8 *da,
  772. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx)
  773. {
  774. struct ieee80211_local *local = sdata->local;
  775. struct sk_buff *skb;
  776. struct ieee80211_mgmt *mgmt;
  777. int err;
  778. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  779. sizeof(*mgmt) + 6 + extra_len);
  780. if (!skb)
  781. return;
  782. skb_reserve(skb, local->hw.extra_tx_headroom);
  783. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  784. memset(mgmt, 0, 24 + 6);
  785. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  786. IEEE80211_STYPE_AUTH);
  787. memcpy(mgmt->da, da, ETH_ALEN);
  788. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  789. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  790. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  791. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  792. mgmt->u.auth.status_code = cpu_to_le16(0);
  793. if (extra)
  794. memcpy(skb_put(skb, extra_len), extra, extra_len);
  795. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  796. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  797. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  798. WARN_ON(err);
  799. }
  800. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  801. ieee80211_tx_skb(sdata, skb);
  802. }
  803. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  804. const u8 *ie, size_t ie_len,
  805. enum ieee80211_band band, u32 rate_mask,
  806. u8 channel)
  807. {
  808. struct ieee80211_supported_band *sband;
  809. u8 *pos;
  810. size_t offset = 0, noffset;
  811. int supp_rates_len, i;
  812. u8 rates[32];
  813. int num_rates;
  814. int ext_rates_len;
  815. sband = local->hw.wiphy->bands[band];
  816. pos = buffer;
  817. num_rates = 0;
  818. for (i = 0; i < sband->n_bitrates; i++) {
  819. if ((BIT(i) & rate_mask) == 0)
  820. continue; /* skip rate */
  821. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  822. }
  823. supp_rates_len = min_t(int, num_rates, 8);
  824. *pos++ = WLAN_EID_SUPP_RATES;
  825. *pos++ = supp_rates_len;
  826. memcpy(pos, rates, supp_rates_len);
  827. pos += supp_rates_len;
  828. /* insert "request information" if in custom IEs */
  829. if (ie && ie_len) {
  830. static const u8 before_extrates[] = {
  831. WLAN_EID_SSID,
  832. WLAN_EID_SUPP_RATES,
  833. WLAN_EID_REQUEST,
  834. };
  835. noffset = ieee80211_ie_split(ie, ie_len,
  836. before_extrates,
  837. ARRAY_SIZE(before_extrates),
  838. offset);
  839. memcpy(pos, ie + offset, noffset - offset);
  840. pos += noffset - offset;
  841. offset = noffset;
  842. }
  843. ext_rates_len = num_rates - supp_rates_len;
  844. if (ext_rates_len > 0) {
  845. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  846. *pos++ = ext_rates_len;
  847. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  848. pos += ext_rates_len;
  849. }
  850. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  851. *pos++ = WLAN_EID_DS_PARAMS;
  852. *pos++ = 1;
  853. *pos++ = channel;
  854. }
  855. /* insert custom IEs that go before HT */
  856. if (ie && ie_len) {
  857. static const u8 before_ht[] = {
  858. WLAN_EID_SSID,
  859. WLAN_EID_SUPP_RATES,
  860. WLAN_EID_REQUEST,
  861. WLAN_EID_EXT_SUPP_RATES,
  862. WLAN_EID_DS_PARAMS,
  863. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  864. };
  865. noffset = ieee80211_ie_split(ie, ie_len,
  866. before_ht, ARRAY_SIZE(before_ht),
  867. offset);
  868. memcpy(pos, ie + offset, noffset - offset);
  869. pos += noffset - offset;
  870. offset = noffset;
  871. }
  872. if (sband->ht_cap.ht_supported)
  873. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  874. sband->ht_cap.cap);
  875. /*
  876. * If adding more here, adjust code in main.c
  877. * that calculates local->scan_ies_len.
  878. */
  879. /* add any remaining custom IEs */
  880. if (ie && ie_len) {
  881. noffset = ie_len;
  882. memcpy(pos, ie + offset, noffset - offset);
  883. pos += noffset - offset;
  884. }
  885. return pos - buffer;
  886. }
  887. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  888. u8 *dst, u32 ratemask,
  889. const u8 *ssid, size_t ssid_len,
  890. const u8 *ie, size_t ie_len,
  891. bool directed)
  892. {
  893. struct ieee80211_local *local = sdata->local;
  894. struct sk_buff *skb;
  895. struct ieee80211_mgmt *mgmt;
  896. size_t buf_len;
  897. u8 *buf;
  898. u8 chan;
  899. /* FIXME: come up with a proper value */
  900. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  901. if (!buf)
  902. return NULL;
  903. /*
  904. * Do not send DS Channel parameter for directed probe requests
  905. * in order to maximize the chance that we get a response. Some
  906. * badly-behaved APs don't respond when this parameter is included.
  907. */
  908. if (directed)
  909. chan = 0;
  910. else
  911. chan = ieee80211_frequency_to_channel(
  912. local->hw.conf.channel->center_freq);
  913. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  914. local->hw.conf.channel->band,
  915. ratemask, chan);
  916. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  917. ssid, ssid_len,
  918. buf, buf_len);
  919. if (!skb)
  920. goto out;
  921. if (dst) {
  922. mgmt = (struct ieee80211_mgmt *) skb->data;
  923. memcpy(mgmt->da, dst, ETH_ALEN);
  924. memcpy(mgmt->bssid, dst, ETH_ALEN);
  925. }
  926. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  927. out:
  928. kfree(buf);
  929. return skb;
  930. }
  931. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  932. const u8 *ssid, size_t ssid_len,
  933. const u8 *ie, size_t ie_len,
  934. u32 ratemask, bool directed, bool no_cck)
  935. {
  936. struct sk_buff *skb;
  937. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  938. ie, ie_len, directed);
  939. if (skb) {
  940. if (no_cck)
  941. IEEE80211_SKB_CB(skb)->flags |=
  942. IEEE80211_TX_CTL_NO_CCK_RATE;
  943. ieee80211_tx_skb(sdata, skb);
  944. }
  945. }
  946. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  947. struct ieee802_11_elems *elems,
  948. enum ieee80211_band band, u32 *basic_rates)
  949. {
  950. struct ieee80211_supported_band *sband;
  951. struct ieee80211_rate *bitrates;
  952. size_t num_rates;
  953. u32 supp_rates;
  954. int i, j;
  955. sband = local->hw.wiphy->bands[band];
  956. if (!sband) {
  957. WARN_ON(1);
  958. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  959. }
  960. bitrates = sband->bitrates;
  961. num_rates = sband->n_bitrates;
  962. supp_rates = 0;
  963. for (i = 0; i < elems->supp_rates_len +
  964. elems->ext_supp_rates_len; i++) {
  965. u8 rate = 0;
  966. int own_rate;
  967. bool is_basic;
  968. if (i < elems->supp_rates_len)
  969. rate = elems->supp_rates[i];
  970. else if (elems->ext_supp_rates)
  971. rate = elems->ext_supp_rates
  972. [i - elems->supp_rates_len];
  973. own_rate = 5 * (rate & 0x7f);
  974. is_basic = !!(rate & 0x80);
  975. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  976. continue;
  977. for (j = 0; j < num_rates; j++) {
  978. if (bitrates[j].bitrate == own_rate) {
  979. supp_rates |= BIT(j);
  980. if (basic_rates && is_basic)
  981. *basic_rates |= BIT(j);
  982. }
  983. }
  984. }
  985. return supp_rates;
  986. }
  987. void ieee80211_stop_device(struct ieee80211_local *local)
  988. {
  989. ieee80211_led_radio(local, false);
  990. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  991. cancel_work_sync(&local->reconfig_filter);
  992. flush_workqueue(local->workqueue);
  993. drv_stop(local);
  994. }
  995. int ieee80211_reconfig(struct ieee80211_local *local)
  996. {
  997. struct ieee80211_hw *hw = &local->hw;
  998. struct ieee80211_sub_if_data *sdata;
  999. struct sta_info *sta;
  1000. int res, i;
  1001. #ifdef CONFIG_PM
  1002. if (local->suspended)
  1003. local->resuming = true;
  1004. if (local->wowlan) {
  1005. local->wowlan = false;
  1006. res = drv_resume(local);
  1007. if (res < 0) {
  1008. local->resuming = false;
  1009. return res;
  1010. }
  1011. if (res == 0)
  1012. goto wake_up;
  1013. WARN_ON(res > 1);
  1014. /*
  1015. * res is 1, which means the driver requested
  1016. * to go through a regular reset on wakeup.
  1017. */
  1018. }
  1019. #endif
  1020. /* everything else happens only if HW was up & running */
  1021. if (!local->open_count)
  1022. goto wake_up;
  1023. /*
  1024. * Upon resume hardware can sometimes be goofy due to
  1025. * various platform / driver / bus issues, so restarting
  1026. * the device may at times not work immediately. Propagate
  1027. * the error.
  1028. */
  1029. res = drv_start(local);
  1030. if (res) {
  1031. WARN(local->suspended, "Hardware became unavailable "
  1032. "upon resume. This could be a software issue "
  1033. "prior to suspend or a hardware issue.\n");
  1034. return res;
  1035. }
  1036. /* setup fragmentation threshold */
  1037. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1038. /* setup RTS threshold */
  1039. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1040. /* reset coverage class */
  1041. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1042. ieee80211_led_radio(local, true);
  1043. ieee80211_mod_tpt_led_trig(local,
  1044. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1045. /* add interfaces */
  1046. list_for_each_entry(sdata, &local->interfaces, list) {
  1047. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1048. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1049. ieee80211_sdata_running(sdata))
  1050. res = drv_add_interface(local, sdata);
  1051. }
  1052. /* add STAs back */
  1053. mutex_lock(&local->sta_mtx);
  1054. list_for_each_entry(sta, &local->sta_list, list) {
  1055. if (sta->uploaded) {
  1056. enum ieee80211_sta_state state;
  1057. for (state = IEEE80211_STA_NOTEXIST;
  1058. state < sta->sta_state - 1; state++)
  1059. WARN_ON(drv_sta_state(local, sta->sdata, sta,
  1060. state, state + 1));
  1061. }
  1062. }
  1063. mutex_unlock(&local->sta_mtx);
  1064. /* reconfigure tx conf */
  1065. if (hw->queues >= IEEE80211_NUM_ACS) {
  1066. list_for_each_entry(sdata, &local->interfaces, list) {
  1067. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1068. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1069. !ieee80211_sdata_running(sdata))
  1070. continue;
  1071. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1072. drv_conf_tx(local, sdata, i,
  1073. &sdata->tx_conf[i]);
  1074. }
  1075. }
  1076. /* reconfigure hardware */
  1077. ieee80211_hw_config(local, ~0);
  1078. ieee80211_configure_filter(local);
  1079. /* Finally also reconfigure all the BSS information */
  1080. list_for_each_entry(sdata, &local->interfaces, list) {
  1081. u32 changed;
  1082. if (!ieee80211_sdata_running(sdata))
  1083. continue;
  1084. /* common change flags for all interface types */
  1085. changed = BSS_CHANGED_ERP_CTS_PROT |
  1086. BSS_CHANGED_ERP_PREAMBLE |
  1087. BSS_CHANGED_ERP_SLOT |
  1088. BSS_CHANGED_HT |
  1089. BSS_CHANGED_BASIC_RATES |
  1090. BSS_CHANGED_BEACON_INT |
  1091. BSS_CHANGED_BSSID |
  1092. BSS_CHANGED_CQM |
  1093. BSS_CHANGED_QOS |
  1094. BSS_CHANGED_IDLE;
  1095. switch (sdata->vif.type) {
  1096. case NL80211_IFTYPE_STATION:
  1097. changed |= BSS_CHANGED_ASSOC |
  1098. BSS_CHANGED_ARP_FILTER;
  1099. mutex_lock(&sdata->u.mgd.mtx);
  1100. ieee80211_bss_info_change_notify(sdata, changed);
  1101. mutex_unlock(&sdata->u.mgd.mtx);
  1102. break;
  1103. case NL80211_IFTYPE_ADHOC:
  1104. changed |= BSS_CHANGED_IBSS;
  1105. /* fall through */
  1106. case NL80211_IFTYPE_AP:
  1107. changed |= BSS_CHANGED_SSID;
  1108. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1109. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1110. /* fall through */
  1111. case NL80211_IFTYPE_MESH_POINT:
  1112. changed |= BSS_CHANGED_BEACON |
  1113. BSS_CHANGED_BEACON_ENABLED;
  1114. ieee80211_bss_info_change_notify(sdata, changed);
  1115. break;
  1116. case NL80211_IFTYPE_WDS:
  1117. break;
  1118. case NL80211_IFTYPE_AP_VLAN:
  1119. case NL80211_IFTYPE_MONITOR:
  1120. /* ignore virtual */
  1121. break;
  1122. case NL80211_IFTYPE_UNSPECIFIED:
  1123. case NUM_NL80211_IFTYPES:
  1124. case NL80211_IFTYPE_P2P_CLIENT:
  1125. case NL80211_IFTYPE_P2P_GO:
  1126. WARN_ON(1);
  1127. break;
  1128. }
  1129. }
  1130. ieee80211_recalc_ps(local, -1);
  1131. /*
  1132. * The sta might be in psm against the ap (e.g. because
  1133. * this was the state before a hw restart), so we
  1134. * explicitly send a null packet in order to make sure
  1135. * it'll sync against the ap (and get out of psm).
  1136. */
  1137. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1138. list_for_each_entry(sdata, &local->interfaces, list) {
  1139. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1140. continue;
  1141. ieee80211_send_nullfunc(local, sdata, 0);
  1142. }
  1143. }
  1144. /*
  1145. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1146. * sessions can be established after a resume.
  1147. *
  1148. * Also tear down aggregation sessions since reconfiguring
  1149. * them in a hardware restart scenario is not easily done
  1150. * right now, and the hardware will have lost information
  1151. * about the sessions, but we and the AP still think they
  1152. * are active. This is really a workaround though.
  1153. */
  1154. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1155. mutex_lock(&local->sta_mtx);
  1156. list_for_each_entry(sta, &local->sta_list, list) {
  1157. ieee80211_sta_tear_down_BA_sessions(sta, true);
  1158. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1159. }
  1160. mutex_unlock(&local->sta_mtx);
  1161. }
  1162. /* add back keys */
  1163. list_for_each_entry(sdata, &local->interfaces, list)
  1164. if (ieee80211_sdata_running(sdata))
  1165. ieee80211_enable_keys(sdata);
  1166. wake_up:
  1167. ieee80211_wake_queues_by_reason(hw,
  1168. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1169. /*
  1170. * If this is for hw restart things are still running.
  1171. * We may want to change that later, however.
  1172. */
  1173. if (!local->suspended)
  1174. return 0;
  1175. #ifdef CONFIG_PM
  1176. /* first set suspended false, then resuming */
  1177. local->suspended = false;
  1178. mb();
  1179. local->resuming = false;
  1180. list_for_each_entry(sdata, &local->interfaces, list) {
  1181. switch(sdata->vif.type) {
  1182. case NL80211_IFTYPE_STATION:
  1183. ieee80211_sta_restart(sdata);
  1184. break;
  1185. case NL80211_IFTYPE_ADHOC:
  1186. ieee80211_ibss_restart(sdata);
  1187. break;
  1188. case NL80211_IFTYPE_MESH_POINT:
  1189. ieee80211_mesh_restart(sdata);
  1190. break;
  1191. default:
  1192. break;
  1193. }
  1194. }
  1195. mod_timer(&local->sta_cleanup, jiffies + 1);
  1196. mutex_lock(&local->sta_mtx);
  1197. list_for_each_entry(sta, &local->sta_list, list)
  1198. mesh_plink_restart(sta);
  1199. mutex_unlock(&local->sta_mtx);
  1200. #else
  1201. WARN_ON(1);
  1202. #endif
  1203. return 0;
  1204. }
  1205. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1206. {
  1207. struct ieee80211_sub_if_data *sdata;
  1208. struct ieee80211_local *local;
  1209. struct ieee80211_key *key;
  1210. if (WARN_ON(!vif))
  1211. return;
  1212. sdata = vif_to_sdata(vif);
  1213. local = sdata->local;
  1214. if (WARN_ON(!local->resuming))
  1215. return;
  1216. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1217. return;
  1218. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1219. mutex_lock(&local->key_mtx);
  1220. list_for_each_entry(key, &sdata->key_list, list)
  1221. key->flags |= KEY_FLAG_TAINTED;
  1222. mutex_unlock(&local->key_mtx);
  1223. }
  1224. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1225. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1226. enum ieee80211_smps_mode *smps_mode)
  1227. {
  1228. if (ifmgd->associated) {
  1229. *smps_mode = ifmgd->ap_smps;
  1230. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1231. if (ifmgd->powersave)
  1232. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1233. else
  1234. *smps_mode = IEEE80211_SMPS_OFF;
  1235. }
  1236. return 1;
  1237. }
  1238. return 0;
  1239. }
  1240. /* must hold iflist_mtx */
  1241. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1242. {
  1243. struct ieee80211_sub_if_data *sdata;
  1244. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1245. int count = 0;
  1246. lockdep_assert_held(&local->iflist_mtx);
  1247. /*
  1248. * This function could be improved to handle multiple
  1249. * interfaces better, but right now it makes any
  1250. * non-station interfaces force SM PS to be turned
  1251. * off. If there are multiple station interfaces it
  1252. * could also use the best possible mode, e.g. if
  1253. * one is in static and the other in dynamic then
  1254. * dynamic is ok.
  1255. */
  1256. list_for_each_entry(sdata, &local->interfaces, list) {
  1257. if (!ieee80211_sdata_running(sdata))
  1258. continue;
  1259. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1260. goto set;
  1261. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1262. if (count > 1) {
  1263. smps_mode = IEEE80211_SMPS_OFF;
  1264. break;
  1265. }
  1266. }
  1267. if (smps_mode == local->smps_mode)
  1268. return;
  1269. set:
  1270. local->smps_mode = smps_mode;
  1271. /* changed flag is auto-detected for this */
  1272. ieee80211_hw_config(local, 0);
  1273. }
  1274. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1275. {
  1276. int i;
  1277. for (i = 0; i < n_ids; i++)
  1278. if (ids[i] == id)
  1279. return true;
  1280. return false;
  1281. }
  1282. /**
  1283. * ieee80211_ie_split - split an IE buffer according to ordering
  1284. *
  1285. * @ies: the IE buffer
  1286. * @ielen: the length of the IE buffer
  1287. * @ids: an array with element IDs that are allowed before
  1288. * the split
  1289. * @n_ids: the size of the element ID array
  1290. * @offset: offset where to start splitting in the buffer
  1291. *
  1292. * This function splits an IE buffer by updating the @offset
  1293. * variable to point to the location where the buffer should be
  1294. * split.
  1295. *
  1296. * It assumes that the given IE buffer is well-formed, this
  1297. * has to be guaranteed by the caller!
  1298. *
  1299. * It also assumes that the IEs in the buffer are ordered
  1300. * correctly, if not the result of using this function will not
  1301. * be ordered correctly either, i.e. it does no reordering.
  1302. *
  1303. * The function returns the offset where the next part of the
  1304. * buffer starts, which may be @ielen if the entire (remainder)
  1305. * of the buffer should be used.
  1306. */
  1307. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1308. const u8 *ids, int n_ids, size_t offset)
  1309. {
  1310. size_t pos = offset;
  1311. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1312. pos += 2 + ies[pos + 1];
  1313. return pos;
  1314. }
  1315. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1316. {
  1317. size_t pos = offset;
  1318. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1319. pos += 2 + ies[pos + 1];
  1320. return pos;
  1321. }
  1322. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1323. int rssi_min_thold,
  1324. int rssi_max_thold)
  1325. {
  1326. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1327. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1328. return;
  1329. /*
  1330. * Scale up threshold values before storing it, as the RSSI averaging
  1331. * algorithm uses a scaled up value as well. Change this scaling
  1332. * factor if the RSSI averaging algorithm changes.
  1333. */
  1334. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1335. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1336. }
  1337. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1338. int rssi_min_thold,
  1339. int rssi_max_thold)
  1340. {
  1341. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1342. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1343. rssi_min_thold > rssi_max_thold);
  1344. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1345. rssi_max_thold);
  1346. }
  1347. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1348. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1349. {
  1350. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1351. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1352. }
  1353. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1354. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1355. u16 cap)
  1356. {
  1357. __le16 tmp;
  1358. *pos++ = WLAN_EID_HT_CAPABILITY;
  1359. *pos++ = sizeof(struct ieee80211_ht_cap);
  1360. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1361. /* capability flags */
  1362. tmp = cpu_to_le16(cap);
  1363. memcpy(pos, &tmp, sizeof(u16));
  1364. pos += sizeof(u16);
  1365. /* AMPDU parameters */
  1366. *pos++ = ht_cap->ampdu_factor |
  1367. (ht_cap->ampdu_density <<
  1368. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1369. /* MCS set */
  1370. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1371. pos += sizeof(ht_cap->mcs);
  1372. /* extended capabilities */
  1373. pos += sizeof(__le16);
  1374. /* BF capabilities */
  1375. pos += sizeof(__le32);
  1376. /* antenna selection */
  1377. pos += sizeof(u8);
  1378. return pos;
  1379. }
  1380. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1381. struct ieee80211_channel *channel,
  1382. enum nl80211_channel_type channel_type)
  1383. {
  1384. struct ieee80211_ht_operation *ht_oper;
  1385. /* Build HT Information */
  1386. *pos++ = WLAN_EID_HT_OPERATION;
  1387. *pos++ = sizeof(struct ieee80211_ht_operation);
  1388. ht_oper = (struct ieee80211_ht_operation *)pos;
  1389. ht_oper->primary_chan =
  1390. ieee80211_frequency_to_channel(channel->center_freq);
  1391. switch (channel_type) {
  1392. case NL80211_CHAN_HT40MINUS:
  1393. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1394. break;
  1395. case NL80211_CHAN_HT40PLUS:
  1396. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1397. break;
  1398. case NL80211_CHAN_HT20:
  1399. default:
  1400. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1401. break;
  1402. }
  1403. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
  1404. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1405. /*
  1406. * Note: According to 802.11n-2009 9.13.3.1, HT Protection field and
  1407. * RIFS Mode are reserved in IBSS mode, therefore keep them at 0
  1408. */
  1409. ht_oper->operation_mode = 0x0000;
  1410. ht_oper->stbc_param = 0x0000;
  1411. /* It seems that Basic MCS set and Supported MCS set
  1412. are identical for the first 10 bytes */
  1413. memset(&ht_oper->basic_set, 0, 16);
  1414. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1415. return pos + sizeof(struct ieee80211_ht_operation);
  1416. }
  1417. enum nl80211_channel_type
  1418. ieee80211_ht_oper_to_channel_type(struct ieee80211_ht_operation *ht_oper)
  1419. {
  1420. enum nl80211_channel_type channel_type;
  1421. if (!ht_oper)
  1422. return NL80211_CHAN_NO_HT;
  1423. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1424. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1425. channel_type = NL80211_CHAN_HT20;
  1426. break;
  1427. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1428. channel_type = NL80211_CHAN_HT40PLUS;
  1429. break;
  1430. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1431. channel_type = NL80211_CHAN_HT40MINUS;
  1432. break;
  1433. default:
  1434. channel_type = NL80211_CHAN_NO_HT;
  1435. }
  1436. return channel_type;
  1437. }
  1438. int ieee80211_add_srates_ie(struct ieee80211_vif *vif,
  1439. struct sk_buff *skb, bool need_basic)
  1440. {
  1441. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1442. struct ieee80211_local *local = sdata->local;
  1443. struct ieee80211_supported_band *sband;
  1444. int rate;
  1445. u8 i, rates, *pos;
  1446. u32 basic_rates = vif->bss_conf.basic_rates;
  1447. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1448. rates = sband->n_bitrates;
  1449. if (rates > 8)
  1450. rates = 8;
  1451. if (skb_tailroom(skb) < rates + 2)
  1452. return -ENOMEM;
  1453. pos = skb_put(skb, rates + 2);
  1454. *pos++ = WLAN_EID_SUPP_RATES;
  1455. *pos++ = rates;
  1456. for (i = 0; i < rates; i++) {
  1457. u8 basic = 0;
  1458. if (need_basic && basic_rates & BIT(i))
  1459. basic = 0x80;
  1460. rate = sband->bitrates[i].bitrate;
  1461. *pos++ = basic | (u8) (rate / 5);
  1462. }
  1463. return 0;
  1464. }
  1465. int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif,
  1466. struct sk_buff *skb, bool need_basic)
  1467. {
  1468. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1469. struct ieee80211_local *local = sdata->local;
  1470. struct ieee80211_supported_band *sband;
  1471. int rate;
  1472. u8 i, exrates, *pos;
  1473. u32 basic_rates = vif->bss_conf.basic_rates;
  1474. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1475. exrates = sband->n_bitrates;
  1476. if (exrates > 8)
  1477. exrates -= 8;
  1478. else
  1479. exrates = 0;
  1480. if (skb_tailroom(skb) < exrates + 2)
  1481. return -ENOMEM;
  1482. if (exrates) {
  1483. pos = skb_put(skb, exrates + 2);
  1484. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1485. *pos++ = exrates;
  1486. for (i = 8; i < sband->n_bitrates; i++) {
  1487. u8 basic = 0;
  1488. if (need_basic && basic_rates & BIT(i))
  1489. basic = 0x80;
  1490. rate = sband->bitrates[i].bitrate;
  1491. *pos++ = basic | (u8) (rate / 5);
  1492. }
  1493. }
  1494. return 0;
  1495. }