util.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * utilities for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.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 = tx->skb;
  86. struct ieee80211_hdr *hdr;
  87. do {
  88. hdr = (struct ieee80211_hdr *) skb->data;
  89. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  90. } while ((skb = skb->next));
  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. if (WARN_ON(queue >= hw->queues))
  234. return;
  235. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  236. if (local->queue_stop_reasons[queue] != 0)
  237. /* someone still has this queue stopped */
  238. return;
  239. if (!skb_queue_empty(&local->pending[queue]))
  240. tasklet_schedule(&local->tx_pending_tasklet);
  241. }
  242. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  243. enum queue_stop_reason reason)
  244. {
  245. struct ieee80211_local *local = hw_to_local(hw);
  246. unsigned long flags;
  247. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  248. __ieee80211_wake_queue(hw, queue, reason);
  249. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  250. }
  251. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  252. {
  253. ieee80211_wake_queue_by_reason(hw, queue,
  254. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  255. }
  256. EXPORT_SYMBOL(ieee80211_wake_queue);
  257. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  258. enum queue_stop_reason reason)
  259. {
  260. struct ieee80211_local *local = hw_to_local(hw);
  261. if (WARN_ON(queue >= hw->queues))
  262. return;
  263. __set_bit(reason, &local->queue_stop_reasons[queue]);
  264. }
  265. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  266. enum queue_stop_reason reason)
  267. {
  268. struct ieee80211_local *local = hw_to_local(hw);
  269. unsigned long flags;
  270. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  271. __ieee80211_stop_queue(hw, queue, reason);
  272. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  273. }
  274. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  275. {
  276. ieee80211_stop_queue_by_reason(hw, queue,
  277. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  278. }
  279. EXPORT_SYMBOL(ieee80211_stop_queue);
  280. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  281. struct sk_buff *skb)
  282. {
  283. struct ieee80211_hw *hw = &local->hw;
  284. unsigned long flags;
  285. int queue = skb_get_queue_mapping(skb);
  286. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  287. if (WARN_ON(!info->control.vif)) {
  288. kfree_skb(skb);
  289. return;
  290. }
  291. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  292. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  293. __skb_queue_tail(&local->pending[queue], skb);
  294. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  295. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  296. }
  297. int ieee80211_add_pending_skbs(struct ieee80211_local *local,
  298. struct sk_buff_head *skbs)
  299. {
  300. struct ieee80211_hw *hw = &local->hw;
  301. struct sk_buff *skb;
  302. unsigned long flags;
  303. int queue, ret = 0, i;
  304. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  305. for (i = 0; i < hw->queues; i++)
  306. __ieee80211_stop_queue(hw, i,
  307. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  308. while ((skb = skb_dequeue(skbs))) {
  309. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  310. if (WARN_ON(!info->control.vif)) {
  311. kfree_skb(skb);
  312. continue;
  313. }
  314. ret++;
  315. queue = skb_get_queue_mapping(skb);
  316. __skb_queue_tail(&local->pending[queue], skb);
  317. }
  318. for (i = 0; i < hw->queues; i++)
  319. __ieee80211_wake_queue(hw, i,
  320. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  321. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  322. return ret;
  323. }
  324. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  325. enum queue_stop_reason reason)
  326. {
  327. struct ieee80211_local *local = hw_to_local(hw);
  328. unsigned long flags;
  329. int i;
  330. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  331. for (i = 0; i < hw->queues; i++)
  332. __ieee80211_stop_queue(hw, i, reason);
  333. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  334. }
  335. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  336. {
  337. ieee80211_stop_queues_by_reason(hw,
  338. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  339. }
  340. EXPORT_SYMBOL(ieee80211_stop_queues);
  341. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  342. {
  343. struct ieee80211_local *local = hw_to_local(hw);
  344. unsigned long flags;
  345. int ret;
  346. if (WARN_ON(queue >= hw->queues))
  347. return true;
  348. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  349. ret = !!local->queue_stop_reasons[queue];
  350. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  351. return ret;
  352. }
  353. EXPORT_SYMBOL(ieee80211_queue_stopped);
  354. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  355. enum queue_stop_reason reason)
  356. {
  357. struct ieee80211_local *local = hw_to_local(hw);
  358. unsigned long flags;
  359. int i;
  360. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  361. for (i = 0; i < hw->queues; i++)
  362. __ieee80211_wake_queue(hw, i, reason);
  363. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  364. }
  365. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  366. {
  367. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  368. }
  369. EXPORT_SYMBOL(ieee80211_wake_queues);
  370. void ieee80211_iterate_active_interfaces(
  371. struct ieee80211_hw *hw,
  372. void (*iterator)(void *data, u8 *mac,
  373. struct ieee80211_vif *vif),
  374. void *data)
  375. {
  376. struct ieee80211_local *local = hw_to_local(hw);
  377. struct ieee80211_sub_if_data *sdata;
  378. mutex_lock(&local->iflist_mtx);
  379. list_for_each_entry(sdata, &local->interfaces, list) {
  380. switch (sdata->vif.type) {
  381. case __NL80211_IFTYPE_AFTER_LAST:
  382. case NL80211_IFTYPE_UNSPECIFIED:
  383. case NL80211_IFTYPE_MONITOR:
  384. case NL80211_IFTYPE_AP_VLAN:
  385. continue;
  386. case NL80211_IFTYPE_AP:
  387. case NL80211_IFTYPE_STATION:
  388. case NL80211_IFTYPE_ADHOC:
  389. case NL80211_IFTYPE_WDS:
  390. case NL80211_IFTYPE_MESH_POINT:
  391. break;
  392. }
  393. if (netif_running(sdata->dev))
  394. iterator(data, sdata->dev->dev_addr,
  395. &sdata->vif);
  396. }
  397. mutex_unlock(&local->iflist_mtx);
  398. }
  399. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  400. void ieee80211_iterate_active_interfaces_atomic(
  401. struct ieee80211_hw *hw,
  402. void (*iterator)(void *data, u8 *mac,
  403. struct ieee80211_vif *vif),
  404. void *data)
  405. {
  406. struct ieee80211_local *local = hw_to_local(hw);
  407. struct ieee80211_sub_if_data *sdata;
  408. rcu_read_lock();
  409. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  410. switch (sdata->vif.type) {
  411. case __NL80211_IFTYPE_AFTER_LAST:
  412. case NL80211_IFTYPE_UNSPECIFIED:
  413. case NL80211_IFTYPE_MONITOR:
  414. case NL80211_IFTYPE_AP_VLAN:
  415. continue;
  416. case NL80211_IFTYPE_AP:
  417. case NL80211_IFTYPE_STATION:
  418. case NL80211_IFTYPE_ADHOC:
  419. case NL80211_IFTYPE_WDS:
  420. case NL80211_IFTYPE_MESH_POINT:
  421. break;
  422. }
  423. if (netif_running(sdata->dev))
  424. iterator(data, sdata->dev->dev_addr,
  425. &sdata->vif);
  426. }
  427. rcu_read_unlock();
  428. }
  429. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  430. /*
  431. * Nothing should have been stuffed into the workqueue during
  432. * the suspend->resume cycle. If this WARN is seen then there
  433. * is a bug with either the driver suspend or something in
  434. * mac80211 stuffing into the workqueue which we haven't yet
  435. * cleared during mac80211's suspend cycle.
  436. */
  437. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  438. {
  439. if (WARN(local->suspended && !local->resuming,
  440. "queueing ieee80211 work while going to suspend\n"))
  441. return false;
  442. return true;
  443. }
  444. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  445. {
  446. struct ieee80211_local *local = hw_to_local(hw);
  447. if (!ieee80211_can_queue_work(local))
  448. return;
  449. queue_work(local->workqueue, work);
  450. }
  451. EXPORT_SYMBOL(ieee80211_queue_work);
  452. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  453. struct delayed_work *dwork,
  454. unsigned long delay)
  455. {
  456. struct ieee80211_local *local = hw_to_local(hw);
  457. if (!ieee80211_can_queue_work(local))
  458. return;
  459. queue_delayed_work(local->workqueue, dwork, delay);
  460. }
  461. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  462. void ieee802_11_parse_elems(u8 *start, size_t len,
  463. struct ieee802_11_elems *elems)
  464. {
  465. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  466. }
  467. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  468. struct ieee802_11_elems *elems,
  469. u64 filter, u32 crc)
  470. {
  471. size_t left = len;
  472. u8 *pos = start;
  473. bool calc_crc = filter != 0;
  474. memset(elems, 0, sizeof(*elems));
  475. elems->ie_start = start;
  476. elems->total_len = len;
  477. while (left >= 2) {
  478. u8 id, elen;
  479. id = *pos++;
  480. elen = *pos++;
  481. left -= 2;
  482. if (elen > left)
  483. break;
  484. if (calc_crc && id < 64 && (filter & BIT(id)))
  485. crc = crc32_be(crc, pos - 2, elen + 2);
  486. switch (id) {
  487. case WLAN_EID_SSID:
  488. elems->ssid = pos;
  489. elems->ssid_len = elen;
  490. break;
  491. case WLAN_EID_SUPP_RATES:
  492. elems->supp_rates = pos;
  493. elems->supp_rates_len = elen;
  494. break;
  495. case WLAN_EID_FH_PARAMS:
  496. elems->fh_params = pos;
  497. elems->fh_params_len = elen;
  498. break;
  499. case WLAN_EID_DS_PARAMS:
  500. elems->ds_params = pos;
  501. elems->ds_params_len = elen;
  502. break;
  503. case WLAN_EID_CF_PARAMS:
  504. elems->cf_params = pos;
  505. elems->cf_params_len = elen;
  506. break;
  507. case WLAN_EID_TIM:
  508. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  509. elems->tim = (void *)pos;
  510. elems->tim_len = elen;
  511. }
  512. break;
  513. case WLAN_EID_IBSS_PARAMS:
  514. elems->ibss_params = pos;
  515. elems->ibss_params_len = elen;
  516. break;
  517. case WLAN_EID_CHALLENGE:
  518. elems->challenge = pos;
  519. elems->challenge_len = elen;
  520. break;
  521. case WLAN_EID_VENDOR_SPECIFIC:
  522. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  523. pos[2] == 0xf2) {
  524. /* Microsoft OUI (00:50:F2) */
  525. if (calc_crc)
  526. crc = crc32_be(crc, pos - 2, elen + 2);
  527. if (pos[3] == 1) {
  528. /* OUI Type 1 - WPA IE */
  529. elems->wpa = pos;
  530. elems->wpa_len = elen;
  531. } else if (elen >= 5 && pos[3] == 2) {
  532. /* OUI Type 2 - WMM IE */
  533. if (pos[4] == 0) {
  534. elems->wmm_info = pos;
  535. elems->wmm_info_len = elen;
  536. } else if (pos[4] == 1) {
  537. elems->wmm_param = pos;
  538. elems->wmm_param_len = elen;
  539. }
  540. }
  541. }
  542. break;
  543. case WLAN_EID_RSN:
  544. elems->rsn = pos;
  545. elems->rsn_len = elen;
  546. break;
  547. case WLAN_EID_ERP_INFO:
  548. elems->erp_info = pos;
  549. elems->erp_info_len = elen;
  550. break;
  551. case WLAN_EID_EXT_SUPP_RATES:
  552. elems->ext_supp_rates = pos;
  553. elems->ext_supp_rates_len = elen;
  554. break;
  555. case WLAN_EID_HT_CAPABILITY:
  556. if (elen >= sizeof(struct ieee80211_ht_cap))
  557. elems->ht_cap_elem = (void *)pos;
  558. break;
  559. case WLAN_EID_HT_INFORMATION:
  560. if (elen >= sizeof(struct ieee80211_ht_info))
  561. elems->ht_info_elem = (void *)pos;
  562. break;
  563. case WLAN_EID_MESH_ID:
  564. elems->mesh_id = pos;
  565. elems->mesh_id_len = elen;
  566. break;
  567. case WLAN_EID_MESH_CONFIG:
  568. elems->mesh_config = pos;
  569. elems->mesh_config_len = elen;
  570. break;
  571. case WLAN_EID_PEER_LINK:
  572. elems->peer_link = pos;
  573. elems->peer_link_len = elen;
  574. break;
  575. case WLAN_EID_PREQ:
  576. elems->preq = pos;
  577. elems->preq_len = elen;
  578. break;
  579. case WLAN_EID_PREP:
  580. elems->prep = pos;
  581. elems->prep_len = elen;
  582. break;
  583. case WLAN_EID_PERR:
  584. elems->perr = pos;
  585. elems->perr_len = elen;
  586. break;
  587. case WLAN_EID_CHANNEL_SWITCH:
  588. elems->ch_switch_elem = pos;
  589. elems->ch_switch_elem_len = elen;
  590. break;
  591. case WLAN_EID_QUIET:
  592. if (!elems->quiet_elem) {
  593. elems->quiet_elem = pos;
  594. elems->quiet_elem_len = elen;
  595. }
  596. elems->num_of_quiet_elem++;
  597. break;
  598. case WLAN_EID_COUNTRY:
  599. elems->country_elem = pos;
  600. elems->country_elem_len = elen;
  601. break;
  602. case WLAN_EID_PWR_CONSTRAINT:
  603. elems->pwr_constr_elem = pos;
  604. elems->pwr_constr_elem_len = elen;
  605. break;
  606. case WLAN_EID_TIMEOUT_INTERVAL:
  607. elems->timeout_int = pos;
  608. elems->timeout_int_len = elen;
  609. break;
  610. default:
  611. break;
  612. }
  613. left -= elen;
  614. pos += elen;
  615. }
  616. return crc;
  617. }
  618. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  619. {
  620. struct ieee80211_local *local = sdata->local;
  621. struct ieee80211_tx_queue_params qparam;
  622. int queue;
  623. bool use_11b;
  624. int aCWmin, aCWmax;
  625. if (!local->ops->conf_tx)
  626. return;
  627. memset(&qparam, 0, sizeof(qparam));
  628. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  629. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  630. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  631. /* Set defaults according to 802.11-2007 Table 7-37 */
  632. aCWmax = 1023;
  633. if (use_11b)
  634. aCWmin = 31;
  635. else
  636. aCWmin = 15;
  637. switch (queue) {
  638. case 3: /* AC_BK */
  639. qparam.cw_max = aCWmax;
  640. qparam.cw_min = aCWmin;
  641. qparam.txop = 0;
  642. qparam.aifs = 7;
  643. break;
  644. default: /* never happens but let's not leave undefined */
  645. case 2: /* AC_BE */
  646. qparam.cw_max = aCWmax;
  647. qparam.cw_min = aCWmin;
  648. qparam.txop = 0;
  649. qparam.aifs = 3;
  650. break;
  651. case 1: /* AC_VI */
  652. qparam.cw_max = aCWmin;
  653. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  654. if (use_11b)
  655. qparam.txop = 6016/32;
  656. else
  657. qparam.txop = 3008/32;
  658. qparam.aifs = 2;
  659. break;
  660. case 0: /* AC_VO */
  661. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  662. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  663. if (use_11b)
  664. qparam.txop = 3264/32;
  665. else
  666. qparam.txop = 1504/32;
  667. qparam.aifs = 2;
  668. break;
  669. }
  670. drv_conf_tx(local, queue, &qparam);
  671. }
  672. }
  673. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  674. const size_t supp_rates_len,
  675. const u8 *supp_rates)
  676. {
  677. struct ieee80211_local *local = sdata->local;
  678. int i, have_higher_than_11mbit = 0;
  679. /* cf. IEEE 802.11 9.2.12 */
  680. for (i = 0; i < supp_rates_len; i++)
  681. if ((supp_rates[i] & 0x7f) * 5 > 110)
  682. have_higher_than_11mbit = 1;
  683. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  684. have_higher_than_11mbit)
  685. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  686. else
  687. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  688. ieee80211_set_wmm_default(sdata);
  689. }
  690. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  691. enum ieee80211_band band)
  692. {
  693. struct ieee80211_supported_band *sband;
  694. struct ieee80211_rate *bitrates;
  695. u32 mandatory_rates;
  696. enum ieee80211_rate_flags mandatory_flag;
  697. int i;
  698. sband = local->hw.wiphy->bands[band];
  699. if (!sband) {
  700. WARN_ON(1);
  701. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  702. }
  703. if (band == IEEE80211_BAND_2GHZ)
  704. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  705. else
  706. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  707. bitrates = sband->bitrates;
  708. mandatory_rates = 0;
  709. for (i = 0; i < sband->n_bitrates; i++)
  710. if (bitrates[i].flags & mandatory_flag)
  711. mandatory_rates |= BIT(i);
  712. return mandatory_rates;
  713. }
  714. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  715. u16 transaction, u16 auth_alg,
  716. u8 *extra, size_t extra_len, const u8 *bssid,
  717. const u8 *key, u8 key_len, u8 key_idx)
  718. {
  719. struct ieee80211_local *local = sdata->local;
  720. struct sk_buff *skb;
  721. struct ieee80211_mgmt *mgmt;
  722. int err;
  723. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  724. sizeof(*mgmt) + 6 + extra_len);
  725. if (!skb) {
  726. printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
  727. "frame\n", sdata->dev->name);
  728. return;
  729. }
  730. skb_reserve(skb, local->hw.extra_tx_headroom);
  731. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  732. memset(mgmt, 0, 24 + 6);
  733. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  734. IEEE80211_STYPE_AUTH);
  735. memcpy(mgmt->da, bssid, ETH_ALEN);
  736. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  737. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  738. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  739. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  740. mgmt->u.auth.status_code = cpu_to_le16(0);
  741. if (extra)
  742. memcpy(skb_put(skb, extra_len), extra, extra_len);
  743. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  744. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  745. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  746. WARN_ON(err);
  747. }
  748. ieee80211_tx_skb(sdata, skb, 0);
  749. }
  750. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  751. const u8 *ie, size_t ie_len)
  752. {
  753. struct ieee80211_supported_band *sband;
  754. u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
  755. int i;
  756. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  757. pos = buffer;
  758. *pos++ = WLAN_EID_SUPP_RATES;
  759. supp_rates_len = pos;
  760. *pos++ = 0;
  761. for (i = 0; i < sband->n_bitrates; i++) {
  762. struct ieee80211_rate *rate = &sband->bitrates[i];
  763. if (esupp_rates_len) {
  764. *esupp_rates_len += 1;
  765. } else if (*supp_rates_len == 8) {
  766. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  767. esupp_rates_len = pos;
  768. *pos++ = 1;
  769. } else
  770. *supp_rates_len += 1;
  771. *pos++ = rate->bitrate / 5;
  772. }
  773. if (sband->ht_cap.ht_supported) {
  774. __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
  775. *pos++ = WLAN_EID_HT_CAPABILITY;
  776. *pos++ = sizeof(struct ieee80211_ht_cap);
  777. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  778. memcpy(pos, &tmp, sizeof(u16));
  779. pos += sizeof(u16);
  780. /* TODO: needs a define here for << 2 */
  781. *pos++ = sband->ht_cap.ampdu_factor |
  782. (sband->ht_cap.ampdu_density << 2);
  783. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  784. pos += sizeof(sband->ht_cap.mcs);
  785. pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
  786. }
  787. /*
  788. * If adding more here, adjust code in main.c
  789. * that calculates local->scan_ies_len.
  790. */
  791. if (ie) {
  792. memcpy(pos, ie, ie_len);
  793. pos += ie_len;
  794. }
  795. return pos - buffer;
  796. }
  797. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  798. const u8 *ssid, size_t ssid_len,
  799. const u8 *ie, size_t ie_len)
  800. {
  801. struct ieee80211_local *local = sdata->local;
  802. struct sk_buff *skb;
  803. struct ieee80211_mgmt *mgmt;
  804. u8 *pos;
  805. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
  806. ie_len);
  807. if (!skb) {
  808. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  809. "request\n", sdata->dev->name);
  810. return;
  811. }
  812. skb_reserve(skb, local->hw.extra_tx_headroom);
  813. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  814. memset(mgmt, 0, 24);
  815. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  816. IEEE80211_STYPE_PROBE_REQ);
  817. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  818. if (dst) {
  819. memcpy(mgmt->da, dst, ETH_ALEN);
  820. memcpy(mgmt->bssid, dst, ETH_ALEN);
  821. } else {
  822. memset(mgmt->da, 0xff, ETH_ALEN);
  823. memset(mgmt->bssid, 0xff, ETH_ALEN);
  824. }
  825. pos = skb_put(skb, 2 + ssid_len);
  826. *pos++ = WLAN_EID_SSID;
  827. *pos++ = ssid_len;
  828. memcpy(pos, ssid, ssid_len);
  829. pos += ssid_len;
  830. skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
  831. ieee80211_tx_skb(sdata, skb, 0);
  832. }
  833. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  834. struct ieee802_11_elems *elems,
  835. enum ieee80211_band band)
  836. {
  837. struct ieee80211_supported_band *sband;
  838. struct ieee80211_rate *bitrates;
  839. size_t num_rates;
  840. u32 supp_rates;
  841. int i, j;
  842. sband = local->hw.wiphy->bands[band];
  843. if (!sband) {
  844. WARN_ON(1);
  845. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  846. }
  847. bitrates = sband->bitrates;
  848. num_rates = sband->n_bitrates;
  849. supp_rates = 0;
  850. for (i = 0; i < elems->supp_rates_len +
  851. elems->ext_supp_rates_len; i++) {
  852. u8 rate = 0;
  853. int own_rate;
  854. if (i < elems->supp_rates_len)
  855. rate = elems->supp_rates[i];
  856. else if (elems->ext_supp_rates)
  857. rate = elems->ext_supp_rates
  858. [i - elems->supp_rates_len];
  859. own_rate = 5 * (rate & 0x7f);
  860. for (j = 0; j < num_rates; j++)
  861. if (bitrates[j].bitrate == own_rate)
  862. supp_rates |= BIT(j);
  863. }
  864. return supp_rates;
  865. }
  866. void ieee80211_stop_device(struct ieee80211_local *local)
  867. {
  868. ieee80211_led_radio(local, false);
  869. cancel_work_sync(&local->reconfig_filter);
  870. drv_stop(local);
  871. flush_workqueue(local->workqueue);
  872. }
  873. int ieee80211_reconfig(struct ieee80211_local *local)
  874. {
  875. struct ieee80211_hw *hw = &local->hw;
  876. struct ieee80211_sub_if_data *sdata;
  877. struct ieee80211_if_init_conf conf;
  878. struct sta_info *sta;
  879. unsigned long flags;
  880. int res;
  881. if (local->suspended)
  882. local->resuming = true;
  883. /* restart hardware */
  884. if (local->open_count) {
  885. res = drv_start(local);
  886. ieee80211_led_radio(local, true);
  887. }
  888. /* add interfaces */
  889. list_for_each_entry(sdata, &local->interfaces, list) {
  890. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  891. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  892. netif_running(sdata->dev)) {
  893. conf.vif = &sdata->vif;
  894. conf.type = sdata->vif.type;
  895. conf.mac_addr = sdata->dev->dev_addr;
  896. res = drv_add_interface(local, &conf);
  897. }
  898. }
  899. /* add STAs back */
  900. if (local->ops->sta_notify) {
  901. spin_lock_irqsave(&local->sta_lock, flags);
  902. list_for_each_entry(sta, &local->sta_list, list) {
  903. sdata = sta->sdata;
  904. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  905. sdata = container_of(sdata->bss,
  906. struct ieee80211_sub_if_data,
  907. u.ap);
  908. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
  909. &sta->sta);
  910. }
  911. spin_unlock_irqrestore(&local->sta_lock, flags);
  912. }
  913. /* Clear Suspend state so that ADDBA requests can be processed */
  914. rcu_read_lock();
  915. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  916. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  917. clear_sta_flags(sta, WLAN_STA_SUSPEND);
  918. }
  919. }
  920. rcu_read_unlock();
  921. /* setup RTS threshold */
  922. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  923. /* reconfigure hardware */
  924. ieee80211_hw_config(local, ~0);
  925. ieee80211_configure_filter(local);
  926. /* Finally also reconfigure all the BSS information */
  927. list_for_each_entry(sdata, &local->interfaces, list) {
  928. u32 changed = ~0;
  929. if (!netif_running(sdata->dev))
  930. continue;
  931. switch (sdata->vif.type) {
  932. case NL80211_IFTYPE_STATION:
  933. /* disable beacon change bits */
  934. changed &= ~(BSS_CHANGED_BEACON |
  935. BSS_CHANGED_BEACON_ENABLED);
  936. /* fall through */
  937. case NL80211_IFTYPE_ADHOC:
  938. case NL80211_IFTYPE_AP:
  939. case NL80211_IFTYPE_MESH_POINT:
  940. ieee80211_bss_info_change_notify(sdata, changed);
  941. break;
  942. case NL80211_IFTYPE_WDS:
  943. break;
  944. case NL80211_IFTYPE_AP_VLAN:
  945. case NL80211_IFTYPE_MONITOR:
  946. /* ignore virtual */
  947. break;
  948. case NL80211_IFTYPE_UNSPECIFIED:
  949. case __NL80211_IFTYPE_AFTER_LAST:
  950. WARN_ON(1);
  951. break;
  952. }
  953. }
  954. /* add back keys */
  955. list_for_each_entry(sdata, &local->interfaces, list)
  956. if (netif_running(sdata->dev))
  957. ieee80211_enable_keys(sdata);
  958. ieee80211_wake_queues_by_reason(hw,
  959. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  960. /*
  961. * If this is for hw restart things are still running.
  962. * We may want to change that later, however.
  963. */
  964. if (!local->suspended)
  965. return 0;
  966. #ifdef CONFIG_PM
  967. /* first set suspended false, then resuming */
  968. local->suspended = false;
  969. mb();
  970. local->resuming = false;
  971. list_for_each_entry(sdata, &local->interfaces, list) {
  972. switch(sdata->vif.type) {
  973. case NL80211_IFTYPE_STATION:
  974. ieee80211_sta_restart(sdata);
  975. break;
  976. case NL80211_IFTYPE_ADHOC:
  977. ieee80211_ibss_restart(sdata);
  978. break;
  979. case NL80211_IFTYPE_MESH_POINT:
  980. ieee80211_mesh_restart(sdata);
  981. break;
  982. default:
  983. break;
  984. }
  985. }
  986. add_timer(&local->sta_cleanup);
  987. spin_lock_irqsave(&local->sta_lock, flags);
  988. list_for_each_entry(sta, &local->sta_list, list)
  989. mesh_plink_restart(sta);
  990. spin_unlock_irqrestore(&local->sta_lock, flags);
  991. #else
  992. WARN_ON(1);
  993. #endif
  994. return 0;
  995. }