util.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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, "queueing ieee80211 work while "
  440. "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. bool from_suspend = local->suspended;
  882. /*
  883. * We're going to start the hardware, at that point
  884. * we are no longer suspended and can RX frames.
  885. */
  886. local->suspended = false;
  887. /* restart hardware */
  888. if (local->open_count) {
  889. res = drv_start(local);
  890. ieee80211_led_radio(local, true);
  891. }
  892. /* add interfaces */
  893. list_for_each_entry(sdata, &local->interfaces, list) {
  894. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  895. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  896. netif_running(sdata->dev)) {
  897. conf.vif = &sdata->vif;
  898. conf.type = sdata->vif.type;
  899. conf.mac_addr = sdata->dev->dev_addr;
  900. res = drv_add_interface(local, &conf);
  901. }
  902. }
  903. /* add STAs back */
  904. if (local->ops->sta_notify) {
  905. spin_lock_irqsave(&local->sta_lock, flags);
  906. list_for_each_entry(sta, &local->sta_list, list) {
  907. sdata = sta->sdata;
  908. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  909. sdata = container_of(sdata->bss,
  910. struct ieee80211_sub_if_data,
  911. u.ap);
  912. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
  913. &sta->sta);
  914. }
  915. spin_unlock_irqrestore(&local->sta_lock, flags);
  916. }
  917. /* Clear Suspend state so that ADDBA requests can be processed */
  918. rcu_read_lock();
  919. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  920. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  921. clear_sta_flags(sta, WLAN_STA_SUSPEND);
  922. }
  923. }
  924. rcu_read_unlock();
  925. /* setup RTS threshold */
  926. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  927. /* reconfigure hardware */
  928. ieee80211_hw_config(local, ~0);
  929. ieee80211_configure_filter(local);
  930. /* Finally also reconfigure all the BSS information */
  931. list_for_each_entry(sdata, &local->interfaces, list) {
  932. u32 changed = ~0;
  933. if (!netif_running(sdata->dev))
  934. continue;
  935. switch (sdata->vif.type) {
  936. case NL80211_IFTYPE_STATION:
  937. /* disable beacon change bits */
  938. changed &= ~(BSS_CHANGED_BEACON |
  939. BSS_CHANGED_BEACON_ENABLED);
  940. /* fall through */
  941. case NL80211_IFTYPE_ADHOC:
  942. case NL80211_IFTYPE_AP:
  943. case NL80211_IFTYPE_MESH_POINT:
  944. ieee80211_bss_info_change_notify(sdata, changed);
  945. break;
  946. case NL80211_IFTYPE_WDS:
  947. break;
  948. case NL80211_IFTYPE_AP_VLAN:
  949. case NL80211_IFTYPE_MONITOR:
  950. /* ignore virtual */
  951. break;
  952. case NL80211_IFTYPE_UNSPECIFIED:
  953. case __NL80211_IFTYPE_AFTER_LAST:
  954. WARN_ON(1);
  955. break;
  956. }
  957. }
  958. /* add back keys */
  959. list_for_each_entry(sdata, &local->interfaces, list)
  960. if (netif_running(sdata->dev))
  961. ieee80211_enable_keys(sdata);
  962. ieee80211_wake_queues_by_reason(hw,
  963. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  964. /*
  965. * If this is for hw restart things are still running.
  966. * We may want to change that later, however.
  967. */
  968. if (!from_suspend)
  969. return 0;
  970. #ifdef CONFIG_PM
  971. local->suspended = false;
  972. list_for_each_entry(sdata, &local->interfaces, list) {
  973. switch(sdata->vif.type) {
  974. case NL80211_IFTYPE_STATION:
  975. ieee80211_sta_restart(sdata);
  976. break;
  977. case NL80211_IFTYPE_ADHOC:
  978. ieee80211_ibss_restart(sdata);
  979. break;
  980. case NL80211_IFTYPE_MESH_POINT:
  981. ieee80211_mesh_restart(sdata);
  982. break;
  983. default:
  984. break;
  985. }
  986. }
  987. add_timer(&local->sta_cleanup);
  988. spin_lock_irqsave(&local->sta_lock, flags);
  989. list_for_each_entry(sta, &local->sta_list, list)
  990. mesh_plink_restart(sta);
  991. spin_unlock_irqrestore(&local->sta_lock, flags);
  992. #else
  993. WARN_ON(1);
  994. #endif
  995. return 0;
  996. }