util.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. /* privid for wiphys to determine whether they belong to us or not */
  33. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  34. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  35. {
  36. struct ieee80211_local *local;
  37. BUG_ON(!wiphy);
  38. local = wiphy_priv(wiphy);
  39. return &local->hw;
  40. }
  41. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  42. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  43. enum nl80211_iftype type)
  44. {
  45. __le16 fc = hdr->frame_control;
  46. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  47. if (len < 16)
  48. return NULL;
  49. if (ieee80211_is_data(fc)) {
  50. if (len < 24) /* drop incorrect hdr len (data) */
  51. return NULL;
  52. if (ieee80211_has_a4(fc))
  53. return NULL;
  54. if (ieee80211_has_tods(fc))
  55. return hdr->addr1;
  56. if (ieee80211_has_fromds(fc))
  57. return hdr->addr2;
  58. return hdr->addr3;
  59. }
  60. if (ieee80211_is_mgmt(fc)) {
  61. if (len < 24) /* drop incorrect hdr len (mgmt) */
  62. return NULL;
  63. return hdr->addr3;
  64. }
  65. if (ieee80211_is_ctl(fc)) {
  66. if(ieee80211_is_pspoll(fc))
  67. return hdr->addr1;
  68. if (ieee80211_is_back_req(fc)) {
  69. switch (type) {
  70. case NL80211_IFTYPE_STATION:
  71. return hdr->addr2;
  72. case NL80211_IFTYPE_AP:
  73. case NL80211_IFTYPE_AP_VLAN:
  74. return hdr->addr1;
  75. default:
  76. break; /* fall through to the return */
  77. }
  78. }
  79. }
  80. return NULL;
  81. }
  82. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  83. {
  84. struct sk_buff *skb = tx->skb;
  85. struct ieee80211_hdr *hdr;
  86. do {
  87. hdr = (struct ieee80211_hdr *) skb->data;
  88. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  89. } while ((skb = skb->next));
  90. }
  91. int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  92. int rate, int erp, int short_preamble)
  93. {
  94. int dur;
  95. /* calculate duration (in microseconds, rounded up to next higher
  96. * integer if it includes a fractional microsecond) to send frame of
  97. * len bytes (does not include FCS) at the given rate. Duration will
  98. * also include SIFS.
  99. *
  100. * rate is in 100 kbps, so divident is multiplied by 10 in the
  101. * DIV_ROUND_UP() operations.
  102. */
  103. if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
  104. /*
  105. * OFDM:
  106. *
  107. * N_DBPS = DATARATE x 4
  108. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  109. * (16 = SIGNAL time, 6 = tail bits)
  110. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  111. *
  112. * T_SYM = 4 usec
  113. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  114. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  115. * signal ext = 6 usec
  116. */
  117. dur = 16; /* SIFS + signal ext */
  118. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  119. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  120. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  121. 4 * rate); /* T_SYM x N_SYM */
  122. } else {
  123. /*
  124. * 802.11b or 802.11g with 802.11b compatibility:
  125. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  126. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  127. *
  128. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  129. * aSIFSTime = 10 usec
  130. * aPreambleLength = 144 usec or 72 usec with short preamble
  131. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  132. */
  133. dur = 10; /* aSIFSTime = 10 usec */
  134. dur += short_preamble ? (72 + 24) : (144 + 48);
  135. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  136. }
  137. return dur;
  138. }
  139. /* Exported duration function for driver use */
  140. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  141. struct ieee80211_vif *vif,
  142. size_t frame_len,
  143. struct ieee80211_rate *rate)
  144. {
  145. struct ieee80211_local *local = hw_to_local(hw);
  146. struct ieee80211_sub_if_data *sdata;
  147. u16 dur;
  148. int erp;
  149. bool short_preamble = false;
  150. erp = 0;
  151. if (vif) {
  152. sdata = vif_to_sdata(vif);
  153. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  154. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  155. erp = rate->flags & IEEE80211_RATE_ERP_G;
  156. }
  157. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
  158. short_preamble);
  159. return cpu_to_le16(dur);
  160. }
  161. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  162. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  163. struct ieee80211_vif *vif, size_t frame_len,
  164. const struct ieee80211_tx_info *frame_txctl)
  165. {
  166. struct ieee80211_local *local = hw_to_local(hw);
  167. struct ieee80211_rate *rate;
  168. struct ieee80211_sub_if_data *sdata;
  169. bool short_preamble;
  170. int erp;
  171. u16 dur;
  172. struct ieee80211_supported_band *sband;
  173. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  174. short_preamble = false;
  175. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  176. erp = 0;
  177. if (vif) {
  178. sdata = vif_to_sdata(vif);
  179. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  180. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  181. erp = rate->flags & IEEE80211_RATE_ERP_G;
  182. }
  183. /* CTS duration */
  184. dur = ieee80211_frame_duration(local, 10, rate->bitrate,
  185. erp, short_preamble);
  186. /* Data frame duration */
  187. dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
  188. erp, short_preamble);
  189. /* ACK duration */
  190. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  191. erp, short_preamble);
  192. return cpu_to_le16(dur);
  193. }
  194. EXPORT_SYMBOL(ieee80211_rts_duration);
  195. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  196. struct ieee80211_vif *vif,
  197. size_t frame_len,
  198. const struct ieee80211_tx_info *frame_txctl)
  199. {
  200. struct ieee80211_local *local = hw_to_local(hw);
  201. struct ieee80211_rate *rate;
  202. struct ieee80211_sub_if_data *sdata;
  203. bool short_preamble;
  204. int erp;
  205. u16 dur;
  206. struct ieee80211_supported_band *sband;
  207. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  208. short_preamble = false;
  209. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  210. erp = 0;
  211. if (vif) {
  212. sdata = vif_to_sdata(vif);
  213. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  214. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  215. erp = rate->flags & IEEE80211_RATE_ERP_G;
  216. }
  217. /* Data frame duration */
  218. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
  219. erp, short_preamble);
  220. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  221. /* ACK duration */
  222. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  223. erp, short_preamble);
  224. }
  225. return cpu_to_le16(dur);
  226. }
  227. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  228. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  229. enum queue_stop_reason reason)
  230. {
  231. struct ieee80211_local *local = hw_to_local(hw);
  232. if (WARN_ON(queue >= hw->queues))
  233. return;
  234. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  235. if (!skb_queue_empty(&local->pending[queue]) &&
  236. local->queue_stop_reasons[queue] ==
  237. BIT(IEEE80211_QUEUE_STOP_REASON_PENDING))
  238. tasklet_schedule(&local->tx_pending_tasklet);
  239. if (local->queue_stop_reasons[queue] != 0)
  240. /* someone still has this queue stopped */
  241. return;
  242. netif_wake_subqueue(local->mdev, queue);
  243. }
  244. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  245. enum queue_stop_reason reason)
  246. {
  247. struct ieee80211_local *local = hw_to_local(hw);
  248. unsigned long flags;
  249. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  250. __ieee80211_wake_queue(hw, queue, reason);
  251. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  252. }
  253. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  254. {
  255. ieee80211_wake_queue_by_reason(hw, queue,
  256. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  257. }
  258. EXPORT_SYMBOL(ieee80211_wake_queue);
  259. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  260. enum queue_stop_reason reason)
  261. {
  262. struct ieee80211_local *local = hw_to_local(hw);
  263. if (WARN_ON(queue >= hw->queues))
  264. return;
  265. /*
  266. * Only stop if it was previously running, this is necessary
  267. * for correct pending packets handling because there we may
  268. * start (but not wake) the queue and rely on that.
  269. */
  270. if (!local->queue_stop_reasons[queue])
  271. netif_stop_subqueue(local->mdev, queue);
  272. __set_bit(reason, &local->queue_stop_reasons[queue]);
  273. }
  274. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  275. enum queue_stop_reason reason)
  276. {
  277. struct ieee80211_local *local = hw_to_local(hw);
  278. unsigned long flags;
  279. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  280. __ieee80211_stop_queue(hw, queue, reason);
  281. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  282. }
  283. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  284. {
  285. ieee80211_stop_queue_by_reason(hw, queue,
  286. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  287. }
  288. EXPORT_SYMBOL(ieee80211_stop_queue);
  289. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  290. struct sk_buff *skb)
  291. {
  292. struct ieee80211_hw *hw = &local->hw;
  293. unsigned long flags;
  294. int queue = skb_get_queue_mapping(skb);
  295. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  296. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  297. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_PENDING);
  298. skb_queue_tail(&local->pending[queue], skb);
  299. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  300. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  301. }
  302. int ieee80211_add_pending_skbs(struct ieee80211_local *local,
  303. struct sk_buff_head *skbs)
  304. {
  305. struct ieee80211_hw *hw = &local->hw;
  306. struct sk_buff *skb;
  307. unsigned long flags;
  308. int queue, ret = 0, i;
  309. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  310. for (i = 0; i < hw->queues; i++)
  311. __ieee80211_stop_queue(hw, i,
  312. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  313. while ((skb = skb_dequeue(skbs))) {
  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. if (ret)
  320. __ieee80211_stop_queue(hw, i,
  321. IEEE80211_QUEUE_STOP_REASON_PENDING);
  322. __ieee80211_wake_queue(hw, i,
  323. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  324. }
  325. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  326. return ret;
  327. }
  328. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  329. enum queue_stop_reason reason)
  330. {
  331. struct ieee80211_local *local = hw_to_local(hw);
  332. unsigned long flags;
  333. int i;
  334. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  335. for (i = 0; i < hw->queues; i++)
  336. __ieee80211_stop_queue(hw, i, reason);
  337. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  338. }
  339. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  340. {
  341. ieee80211_stop_queues_by_reason(hw,
  342. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  343. }
  344. EXPORT_SYMBOL(ieee80211_stop_queues);
  345. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  346. {
  347. struct ieee80211_local *local = hw_to_local(hw);
  348. if (WARN_ON(queue >= hw->queues))
  349. return true;
  350. return __netif_subqueue_stopped(local->mdev, queue);
  351. }
  352. EXPORT_SYMBOL(ieee80211_queue_stopped);
  353. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  354. enum queue_stop_reason reason)
  355. {
  356. struct ieee80211_local *local = hw_to_local(hw);
  357. unsigned long flags;
  358. int i;
  359. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  360. for (i = 0; i < hw->queues; i++)
  361. __ieee80211_wake_queue(hw, i, reason);
  362. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  363. }
  364. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  365. {
  366. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  367. }
  368. EXPORT_SYMBOL(ieee80211_wake_queues);
  369. void ieee80211_iterate_active_interfaces(
  370. struct ieee80211_hw *hw,
  371. void (*iterator)(void *data, u8 *mac,
  372. struct ieee80211_vif *vif),
  373. void *data)
  374. {
  375. struct ieee80211_local *local = hw_to_local(hw);
  376. struct ieee80211_sub_if_data *sdata;
  377. mutex_lock(&local->iflist_mtx);
  378. list_for_each_entry(sdata, &local->interfaces, list) {
  379. switch (sdata->vif.type) {
  380. case __NL80211_IFTYPE_AFTER_LAST:
  381. case NL80211_IFTYPE_UNSPECIFIED:
  382. case NL80211_IFTYPE_MONITOR:
  383. case NL80211_IFTYPE_AP_VLAN:
  384. continue;
  385. case NL80211_IFTYPE_AP:
  386. case NL80211_IFTYPE_STATION:
  387. case NL80211_IFTYPE_ADHOC:
  388. case NL80211_IFTYPE_WDS:
  389. case NL80211_IFTYPE_MESH_POINT:
  390. break;
  391. }
  392. if (netif_running(sdata->dev))
  393. iterator(data, sdata->dev->dev_addr,
  394. &sdata->vif);
  395. }
  396. mutex_unlock(&local->iflist_mtx);
  397. }
  398. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  399. void ieee80211_iterate_active_interfaces_atomic(
  400. struct ieee80211_hw *hw,
  401. void (*iterator)(void *data, u8 *mac,
  402. struct ieee80211_vif *vif),
  403. void *data)
  404. {
  405. struct ieee80211_local *local = hw_to_local(hw);
  406. struct ieee80211_sub_if_data *sdata;
  407. rcu_read_lock();
  408. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  409. switch (sdata->vif.type) {
  410. case __NL80211_IFTYPE_AFTER_LAST:
  411. case NL80211_IFTYPE_UNSPECIFIED:
  412. case NL80211_IFTYPE_MONITOR:
  413. case NL80211_IFTYPE_AP_VLAN:
  414. continue;
  415. case NL80211_IFTYPE_AP:
  416. case NL80211_IFTYPE_STATION:
  417. case NL80211_IFTYPE_ADHOC:
  418. case NL80211_IFTYPE_WDS:
  419. case NL80211_IFTYPE_MESH_POINT:
  420. break;
  421. }
  422. if (netif_running(sdata->dev))
  423. iterator(data, sdata->dev->dev_addr,
  424. &sdata->vif);
  425. }
  426. rcu_read_unlock();
  427. }
  428. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  429. void ieee802_11_parse_elems(u8 *start, size_t len,
  430. struct ieee802_11_elems *elems)
  431. {
  432. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  433. }
  434. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  435. struct ieee802_11_elems *elems,
  436. u64 filter, u32 crc)
  437. {
  438. size_t left = len;
  439. u8 *pos = start;
  440. bool calc_crc = filter != 0;
  441. memset(elems, 0, sizeof(*elems));
  442. elems->ie_start = start;
  443. elems->total_len = len;
  444. while (left >= 2) {
  445. u8 id, elen;
  446. id = *pos++;
  447. elen = *pos++;
  448. left -= 2;
  449. if (elen > left)
  450. break;
  451. if (calc_crc && id < 64 && (filter & BIT(id)))
  452. crc = crc32_be(crc, pos - 2, elen + 2);
  453. switch (id) {
  454. case WLAN_EID_SSID:
  455. elems->ssid = pos;
  456. elems->ssid_len = elen;
  457. break;
  458. case WLAN_EID_SUPP_RATES:
  459. elems->supp_rates = pos;
  460. elems->supp_rates_len = elen;
  461. break;
  462. case WLAN_EID_FH_PARAMS:
  463. elems->fh_params = pos;
  464. elems->fh_params_len = elen;
  465. break;
  466. case WLAN_EID_DS_PARAMS:
  467. elems->ds_params = pos;
  468. elems->ds_params_len = elen;
  469. break;
  470. case WLAN_EID_CF_PARAMS:
  471. elems->cf_params = pos;
  472. elems->cf_params_len = elen;
  473. break;
  474. case WLAN_EID_TIM:
  475. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  476. elems->tim = (void *)pos;
  477. elems->tim_len = elen;
  478. }
  479. break;
  480. case WLAN_EID_IBSS_PARAMS:
  481. elems->ibss_params = pos;
  482. elems->ibss_params_len = elen;
  483. break;
  484. case WLAN_EID_CHALLENGE:
  485. elems->challenge = pos;
  486. elems->challenge_len = elen;
  487. break;
  488. case WLAN_EID_VENDOR_SPECIFIC:
  489. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  490. pos[2] == 0xf2) {
  491. /* Microsoft OUI (00:50:F2) */
  492. if (calc_crc)
  493. crc = crc32_be(crc, pos - 2, elen + 2);
  494. if (pos[3] == 1) {
  495. /* OUI Type 1 - WPA IE */
  496. elems->wpa = pos;
  497. elems->wpa_len = elen;
  498. } else if (elen >= 5 && pos[3] == 2) {
  499. /* OUI Type 2 - WMM IE */
  500. if (pos[4] == 0) {
  501. elems->wmm_info = pos;
  502. elems->wmm_info_len = elen;
  503. } else if (pos[4] == 1) {
  504. elems->wmm_param = pos;
  505. elems->wmm_param_len = elen;
  506. }
  507. }
  508. }
  509. break;
  510. case WLAN_EID_RSN:
  511. elems->rsn = pos;
  512. elems->rsn_len = elen;
  513. break;
  514. case WLAN_EID_ERP_INFO:
  515. elems->erp_info = pos;
  516. elems->erp_info_len = elen;
  517. break;
  518. case WLAN_EID_EXT_SUPP_RATES:
  519. elems->ext_supp_rates = pos;
  520. elems->ext_supp_rates_len = elen;
  521. break;
  522. case WLAN_EID_HT_CAPABILITY:
  523. if (elen >= sizeof(struct ieee80211_ht_cap))
  524. elems->ht_cap_elem = (void *)pos;
  525. break;
  526. case WLAN_EID_HT_INFORMATION:
  527. if (elen >= sizeof(struct ieee80211_ht_info))
  528. elems->ht_info_elem = (void *)pos;
  529. break;
  530. case WLAN_EID_MESH_ID:
  531. elems->mesh_id = pos;
  532. elems->mesh_id_len = elen;
  533. break;
  534. case WLAN_EID_MESH_CONFIG:
  535. elems->mesh_config = pos;
  536. elems->mesh_config_len = elen;
  537. break;
  538. case WLAN_EID_PEER_LINK:
  539. elems->peer_link = pos;
  540. elems->peer_link_len = elen;
  541. break;
  542. case WLAN_EID_PREQ:
  543. elems->preq = pos;
  544. elems->preq_len = elen;
  545. break;
  546. case WLAN_EID_PREP:
  547. elems->prep = pos;
  548. elems->prep_len = elen;
  549. break;
  550. case WLAN_EID_PERR:
  551. elems->perr = pos;
  552. elems->perr_len = elen;
  553. break;
  554. case WLAN_EID_CHANNEL_SWITCH:
  555. elems->ch_switch_elem = pos;
  556. elems->ch_switch_elem_len = elen;
  557. break;
  558. case WLAN_EID_QUIET:
  559. if (!elems->quiet_elem) {
  560. elems->quiet_elem = pos;
  561. elems->quiet_elem_len = elen;
  562. }
  563. elems->num_of_quiet_elem++;
  564. break;
  565. case WLAN_EID_COUNTRY:
  566. elems->country_elem = pos;
  567. elems->country_elem_len = elen;
  568. break;
  569. case WLAN_EID_PWR_CONSTRAINT:
  570. elems->pwr_constr_elem = pos;
  571. elems->pwr_constr_elem_len = elen;
  572. break;
  573. case WLAN_EID_TIMEOUT_INTERVAL:
  574. elems->timeout_int = pos;
  575. elems->timeout_int_len = elen;
  576. break;
  577. default:
  578. break;
  579. }
  580. left -= elen;
  581. pos += elen;
  582. }
  583. return crc;
  584. }
  585. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  586. {
  587. struct ieee80211_local *local = sdata->local;
  588. struct ieee80211_tx_queue_params qparam;
  589. int queue;
  590. bool use_11b;
  591. int aCWmin, aCWmax;
  592. if (!local->ops->conf_tx)
  593. return;
  594. memset(&qparam, 0, sizeof(qparam));
  595. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  596. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  597. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  598. /* Set defaults according to 802.11-2007 Table 7-37 */
  599. aCWmax = 1023;
  600. if (use_11b)
  601. aCWmin = 31;
  602. else
  603. aCWmin = 15;
  604. switch (queue) {
  605. case 3: /* AC_BK */
  606. qparam.cw_max = aCWmax;
  607. qparam.cw_min = aCWmin;
  608. qparam.txop = 0;
  609. qparam.aifs = 7;
  610. break;
  611. default: /* never happens but let's not leave undefined */
  612. case 2: /* AC_BE */
  613. qparam.cw_max = aCWmax;
  614. qparam.cw_min = aCWmin;
  615. qparam.txop = 0;
  616. qparam.aifs = 3;
  617. break;
  618. case 1: /* AC_VI */
  619. qparam.cw_max = aCWmin;
  620. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  621. if (use_11b)
  622. qparam.txop = 6016/32;
  623. else
  624. qparam.txop = 3008/32;
  625. qparam.aifs = 2;
  626. break;
  627. case 0: /* AC_VO */
  628. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  629. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  630. if (use_11b)
  631. qparam.txop = 3264/32;
  632. else
  633. qparam.txop = 1504/32;
  634. qparam.aifs = 2;
  635. break;
  636. }
  637. drv_conf_tx(local, queue, &qparam);
  638. }
  639. }
  640. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  641. const size_t supp_rates_len,
  642. const u8 *supp_rates)
  643. {
  644. struct ieee80211_local *local = sdata->local;
  645. int i, have_higher_than_11mbit = 0;
  646. /* cf. IEEE 802.11 9.2.12 */
  647. for (i = 0; i < supp_rates_len; i++)
  648. if ((supp_rates[i] & 0x7f) * 5 > 110)
  649. have_higher_than_11mbit = 1;
  650. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  651. have_higher_than_11mbit)
  652. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  653. else
  654. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  655. ieee80211_set_wmm_default(sdata);
  656. }
  657. void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  658. int encrypt)
  659. {
  660. skb->dev = sdata->local->mdev;
  661. skb_set_mac_header(skb, 0);
  662. skb_set_network_header(skb, 0);
  663. skb_set_transport_header(skb, 0);
  664. skb->iif = sdata->dev->ifindex;
  665. skb->do_not_encrypt = !encrypt;
  666. dev_queue_xmit(skb);
  667. }
  668. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  669. enum ieee80211_band band)
  670. {
  671. struct ieee80211_supported_band *sband;
  672. struct ieee80211_rate *bitrates;
  673. u32 mandatory_rates;
  674. enum ieee80211_rate_flags mandatory_flag;
  675. int i;
  676. sband = local->hw.wiphy->bands[band];
  677. if (!sband) {
  678. WARN_ON(1);
  679. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  680. }
  681. if (band == IEEE80211_BAND_2GHZ)
  682. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  683. else
  684. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  685. bitrates = sband->bitrates;
  686. mandatory_rates = 0;
  687. for (i = 0; i < sband->n_bitrates; i++)
  688. if (bitrates[i].flags & mandatory_flag)
  689. mandatory_rates |= BIT(i);
  690. return mandatory_rates;
  691. }
  692. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  693. u16 transaction, u16 auth_alg,
  694. u8 *extra, size_t extra_len,
  695. const u8 *bssid, int encrypt)
  696. {
  697. struct ieee80211_local *local = sdata->local;
  698. struct sk_buff *skb;
  699. struct ieee80211_mgmt *mgmt;
  700. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  701. sizeof(*mgmt) + 6 + extra_len);
  702. if (!skb) {
  703. printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
  704. "frame\n", sdata->dev->name);
  705. return;
  706. }
  707. skb_reserve(skb, local->hw.extra_tx_headroom);
  708. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  709. memset(mgmt, 0, 24 + 6);
  710. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  711. IEEE80211_STYPE_AUTH);
  712. if (encrypt)
  713. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  714. memcpy(mgmt->da, bssid, ETH_ALEN);
  715. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  716. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  717. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  718. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  719. mgmt->u.auth.status_code = cpu_to_le16(0);
  720. if (extra)
  721. memcpy(skb_put(skb, extra_len), extra, extra_len);
  722. ieee80211_tx_skb(sdata, skb, encrypt);
  723. }
  724. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  725. const u8 *ie, size_t ie_len)
  726. {
  727. struct ieee80211_supported_band *sband;
  728. u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
  729. int i;
  730. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  731. pos = buffer;
  732. *pos++ = WLAN_EID_SUPP_RATES;
  733. supp_rates_len = pos;
  734. *pos++ = 0;
  735. for (i = 0; i < sband->n_bitrates; i++) {
  736. struct ieee80211_rate *rate = &sband->bitrates[i];
  737. if (esupp_rates_len) {
  738. *esupp_rates_len += 1;
  739. } else if (*supp_rates_len == 8) {
  740. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  741. esupp_rates_len = pos;
  742. *pos++ = 1;
  743. } else
  744. *supp_rates_len += 1;
  745. *pos++ = rate->bitrate / 5;
  746. }
  747. if (sband->ht_cap.ht_supported) {
  748. __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
  749. *pos++ = WLAN_EID_HT_CAPABILITY;
  750. *pos++ = sizeof(struct ieee80211_ht_cap);
  751. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  752. memcpy(pos, &tmp, sizeof(u16));
  753. pos += sizeof(u16);
  754. /* TODO: needs a define here for << 2 */
  755. *pos++ = sband->ht_cap.ampdu_factor |
  756. (sband->ht_cap.ampdu_density << 2);
  757. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  758. pos += sizeof(sband->ht_cap.mcs);
  759. pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
  760. }
  761. /*
  762. * If adding more here, adjust code in main.c
  763. * that calculates local->scan_ies_len.
  764. */
  765. if (ie) {
  766. memcpy(pos, ie, ie_len);
  767. pos += ie_len;
  768. }
  769. return pos - buffer;
  770. }
  771. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  772. const u8 *ssid, size_t ssid_len,
  773. const u8 *ie, size_t ie_len)
  774. {
  775. struct ieee80211_local *local = sdata->local;
  776. struct sk_buff *skb;
  777. struct ieee80211_mgmt *mgmt;
  778. u8 *pos;
  779. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
  780. ie_len);
  781. if (!skb) {
  782. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  783. "request\n", sdata->dev->name);
  784. return;
  785. }
  786. skb_reserve(skb, local->hw.extra_tx_headroom);
  787. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  788. memset(mgmt, 0, 24);
  789. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  790. IEEE80211_STYPE_PROBE_REQ);
  791. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  792. if (dst) {
  793. memcpy(mgmt->da, dst, ETH_ALEN);
  794. memcpy(mgmt->bssid, dst, ETH_ALEN);
  795. } else {
  796. memset(mgmt->da, 0xff, ETH_ALEN);
  797. memset(mgmt->bssid, 0xff, ETH_ALEN);
  798. }
  799. pos = skb_put(skb, 2 + ssid_len);
  800. *pos++ = WLAN_EID_SSID;
  801. *pos++ = ssid_len;
  802. memcpy(pos, ssid, ssid_len);
  803. pos += ssid_len;
  804. skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
  805. ieee80211_tx_skb(sdata, skb, 0);
  806. }
  807. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  808. struct ieee802_11_elems *elems,
  809. enum ieee80211_band band)
  810. {
  811. struct ieee80211_supported_band *sband;
  812. struct ieee80211_rate *bitrates;
  813. size_t num_rates;
  814. u32 supp_rates;
  815. int i, j;
  816. sband = local->hw.wiphy->bands[band];
  817. if (!sband) {
  818. WARN_ON(1);
  819. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  820. }
  821. bitrates = sband->bitrates;
  822. num_rates = sband->n_bitrates;
  823. supp_rates = 0;
  824. for (i = 0; i < elems->supp_rates_len +
  825. elems->ext_supp_rates_len; i++) {
  826. u8 rate = 0;
  827. int own_rate;
  828. if (i < elems->supp_rates_len)
  829. rate = elems->supp_rates[i];
  830. else if (elems->ext_supp_rates)
  831. rate = elems->ext_supp_rates
  832. [i - elems->supp_rates_len];
  833. own_rate = 5 * (rate & 0x7f);
  834. for (j = 0; j < num_rates; j++)
  835. if (bitrates[j].bitrate == own_rate)
  836. supp_rates |= BIT(j);
  837. }
  838. return supp_rates;
  839. }
  840. int ieee80211_reconfig(struct ieee80211_local *local)
  841. {
  842. struct ieee80211_hw *hw = &local->hw;
  843. struct ieee80211_sub_if_data *sdata;
  844. struct ieee80211_if_init_conf conf;
  845. struct sta_info *sta;
  846. unsigned long flags;
  847. int res;
  848. bool from_suspend = local->suspended;
  849. /*
  850. * We're going to start the hardware, at that point
  851. * we are no longer suspended and can RX frames.
  852. */
  853. local->suspended = false;
  854. /* restart hardware */
  855. if (local->open_count) {
  856. res = drv_start(local);
  857. ieee80211_led_radio(local, true);
  858. }
  859. /* add interfaces */
  860. list_for_each_entry(sdata, &local->interfaces, list) {
  861. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  862. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  863. netif_running(sdata->dev)) {
  864. conf.vif = &sdata->vif;
  865. conf.type = sdata->vif.type;
  866. conf.mac_addr = sdata->dev->dev_addr;
  867. res = drv_add_interface(local, &conf);
  868. }
  869. }
  870. /* add STAs back */
  871. if (local->ops->sta_notify) {
  872. spin_lock_irqsave(&local->sta_lock, flags);
  873. list_for_each_entry(sta, &local->sta_list, list) {
  874. sdata = sta->sdata;
  875. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  876. sdata = container_of(sdata->bss,
  877. struct ieee80211_sub_if_data,
  878. u.ap);
  879. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
  880. &sta->sta);
  881. }
  882. spin_unlock_irqrestore(&local->sta_lock, flags);
  883. }
  884. /* Clear Suspend state so that ADDBA requests can be processed */
  885. rcu_read_lock();
  886. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  887. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  888. clear_sta_flags(sta, WLAN_STA_SUSPEND);
  889. }
  890. }
  891. rcu_read_unlock();
  892. /* setup RTS threshold */
  893. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  894. /* reconfigure hardware */
  895. ieee80211_hw_config(local, ~0);
  896. netif_addr_lock_bh(local->mdev);
  897. ieee80211_configure_filter(local);
  898. netif_addr_unlock_bh(local->mdev);
  899. /* Finally also reconfigure all the BSS information */
  900. list_for_each_entry(sdata, &local->interfaces, list) {
  901. u32 changed = ~0;
  902. if (!netif_running(sdata->dev))
  903. continue;
  904. switch (sdata->vif.type) {
  905. case NL80211_IFTYPE_STATION:
  906. /* disable beacon change bits */
  907. changed &= ~(BSS_CHANGED_BEACON |
  908. BSS_CHANGED_BEACON_ENABLED);
  909. /* fall through */
  910. case NL80211_IFTYPE_ADHOC:
  911. case NL80211_IFTYPE_AP:
  912. case NL80211_IFTYPE_MESH_POINT:
  913. ieee80211_bss_info_change_notify(sdata, changed);
  914. break;
  915. case NL80211_IFTYPE_WDS:
  916. break;
  917. case NL80211_IFTYPE_AP_VLAN:
  918. case NL80211_IFTYPE_MONITOR:
  919. /* ignore virtual */
  920. break;
  921. case NL80211_IFTYPE_UNSPECIFIED:
  922. case __NL80211_IFTYPE_AFTER_LAST:
  923. WARN_ON(1);
  924. break;
  925. }
  926. }
  927. /* add back keys */
  928. list_for_each_entry(sdata, &local->interfaces, list)
  929. if (netif_running(sdata->dev))
  930. ieee80211_enable_keys(sdata);
  931. ieee80211_wake_queues_by_reason(hw,
  932. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  933. /*
  934. * If this is for hw restart things are still running.
  935. * We may want to change that later, however.
  936. */
  937. if (!from_suspend)
  938. return 0;
  939. #ifdef CONFIG_PM
  940. local->suspended = false;
  941. list_for_each_entry(sdata, &local->interfaces, list) {
  942. switch(sdata->vif.type) {
  943. case NL80211_IFTYPE_STATION:
  944. ieee80211_sta_restart(sdata);
  945. break;
  946. case NL80211_IFTYPE_ADHOC:
  947. ieee80211_ibss_restart(sdata);
  948. break;
  949. case NL80211_IFTYPE_MESH_POINT:
  950. ieee80211_mesh_restart(sdata);
  951. break;
  952. default:
  953. break;
  954. }
  955. }
  956. add_timer(&local->sta_cleanup);
  957. spin_lock_irqsave(&local->sta_lock, flags);
  958. list_for_each_entry(sta, &local->sta_list, list)
  959. mesh_plink_restart(sta);
  960. spin_unlock_irqrestore(&local->sta_lock, flags);
  961. #else
  962. WARN_ON(1);
  963. #endif
  964. return 0;
  965. }