util.c 28 KB

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