util.c 28 KB

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