util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <net/cfg80211.h>
  23. #include "ieee80211_i.h"
  24. #include "ieee80211_rate.h"
  25. #include "wme.h"
  26. /* privid for wiphys to determine whether they belong to us or not */
  27. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  28. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  29. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  30. const unsigned char rfc1042_header[] =
  31. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  32. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  33. const unsigned char bridge_tunnel_header[] =
  34. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  35. /* No encapsulation header if EtherType < 0x600 (=length) */
  36. static const unsigned char eapol_header[] =
  37. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
  38. static int rate_list_match(const int *rate_list, int rate)
  39. {
  40. int i;
  41. if (!rate_list)
  42. return 0;
  43. for (i = 0; rate_list[i] >= 0; i++)
  44. if (rate_list[i] == rate)
  45. return 1;
  46. return 0;
  47. }
  48. void ieee80211_prepare_rates(struct ieee80211_local *local,
  49. struct ieee80211_hw_mode *mode)
  50. {
  51. int i;
  52. for (i = 0; i < mode->num_rates; i++) {
  53. struct ieee80211_rate *rate = &mode->rates[i];
  54. rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
  55. IEEE80211_RATE_BASIC);
  56. if (local->supp_rates[mode->mode]) {
  57. if (!rate_list_match(local->supp_rates[mode->mode],
  58. rate->rate))
  59. continue;
  60. }
  61. rate->flags |= IEEE80211_RATE_SUPPORTED;
  62. /* Use configured basic rate set if it is available. If not,
  63. * use defaults that are sane for most cases. */
  64. if (local->basic_rates[mode->mode]) {
  65. if (rate_list_match(local->basic_rates[mode->mode],
  66. rate->rate))
  67. rate->flags |= IEEE80211_RATE_BASIC;
  68. } else switch (mode->mode) {
  69. case MODE_IEEE80211A:
  70. if (rate->rate == 60 || rate->rate == 120 ||
  71. rate->rate == 240)
  72. rate->flags |= IEEE80211_RATE_BASIC;
  73. break;
  74. case MODE_IEEE80211B:
  75. if (rate->rate == 10 || rate->rate == 20)
  76. rate->flags |= IEEE80211_RATE_BASIC;
  77. break;
  78. case MODE_ATHEROS_TURBO:
  79. if (rate->rate == 120 || rate->rate == 240 ||
  80. rate->rate == 480)
  81. rate->flags |= IEEE80211_RATE_BASIC;
  82. break;
  83. case MODE_IEEE80211G:
  84. if (rate->rate == 10 || rate->rate == 20 ||
  85. rate->rate == 55 || rate->rate == 110)
  86. rate->flags |= IEEE80211_RATE_BASIC;
  87. break;
  88. }
  89. /* Set ERP and MANDATORY flags based on phymode */
  90. switch (mode->mode) {
  91. case MODE_IEEE80211A:
  92. if (rate->rate == 60 || rate->rate == 120 ||
  93. rate->rate == 240)
  94. rate->flags |= IEEE80211_RATE_MANDATORY;
  95. break;
  96. case MODE_IEEE80211B:
  97. if (rate->rate == 10)
  98. rate->flags |= IEEE80211_RATE_MANDATORY;
  99. break;
  100. case MODE_ATHEROS_TURBO:
  101. break;
  102. case MODE_IEEE80211G:
  103. if (rate->rate == 10 || rate->rate == 20 ||
  104. rate->rate == 55 || rate->rate == 110 ||
  105. rate->rate == 60 || rate->rate == 120 ||
  106. rate->rate == 240)
  107. rate->flags |= IEEE80211_RATE_MANDATORY;
  108. break;
  109. }
  110. if (ieee80211_is_erp_rate(mode->mode, rate->rate))
  111. rate->flags |= IEEE80211_RATE_ERP;
  112. }
  113. }
  114. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
  115. {
  116. u16 fc;
  117. if (len < 24)
  118. return NULL;
  119. fc = le16_to_cpu(hdr->frame_control);
  120. switch (fc & IEEE80211_FCTL_FTYPE) {
  121. case IEEE80211_FTYPE_DATA:
  122. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  123. case IEEE80211_FCTL_TODS:
  124. return hdr->addr1;
  125. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  126. return NULL;
  127. case IEEE80211_FCTL_FROMDS:
  128. return hdr->addr2;
  129. case 0:
  130. return hdr->addr3;
  131. }
  132. break;
  133. case IEEE80211_FTYPE_MGMT:
  134. return hdr->addr3;
  135. case IEEE80211_FTYPE_CTL:
  136. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
  137. return hdr->addr1;
  138. else
  139. return NULL;
  140. }
  141. return NULL;
  142. }
  143. int ieee80211_get_hdrlen(u16 fc)
  144. {
  145. int hdrlen = 24;
  146. switch (fc & IEEE80211_FCTL_FTYPE) {
  147. case IEEE80211_FTYPE_DATA:
  148. if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
  149. hdrlen = 30; /* Addr4 */
  150. /*
  151. * The QoS Control field is two bytes and its presence is
  152. * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
  153. * hdrlen if that bit is set.
  154. * This works by masking out the bit and shifting it to
  155. * bit position 1 so the result has the value 0 or 2.
  156. */
  157. hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
  158. >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
  159. break;
  160. case IEEE80211_FTYPE_CTL:
  161. /*
  162. * ACK and CTS are 10 bytes, all others 16. To see how
  163. * to get this condition consider
  164. * subtype mask: 0b0000000011110000 (0x00F0)
  165. * ACK subtype: 0b0000000011010000 (0x00D0)
  166. * CTS subtype: 0b0000000011000000 (0x00C0)
  167. * bits that matter: ^^^ (0x00E0)
  168. * value of those: 0b0000000011000000 (0x00C0)
  169. */
  170. if ((fc & 0xE0) == 0xC0)
  171. hdrlen = 10;
  172. else
  173. hdrlen = 16;
  174. break;
  175. }
  176. return hdrlen;
  177. }
  178. EXPORT_SYMBOL(ieee80211_get_hdrlen);
  179. int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  180. {
  181. const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
  182. int hdrlen;
  183. if (unlikely(skb->len < 10))
  184. return 0;
  185. hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  186. if (unlikely(hdrlen > skb->len))
  187. return 0;
  188. return hdrlen;
  189. }
  190. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  191. int ieee80211_is_eapol(const struct sk_buff *skb)
  192. {
  193. const struct ieee80211_hdr *hdr;
  194. u16 fc;
  195. int hdrlen;
  196. if (unlikely(skb->len < 10))
  197. return 0;
  198. hdr = (const struct ieee80211_hdr *) skb->data;
  199. fc = le16_to_cpu(hdr->frame_control);
  200. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  201. return 0;
  202. hdrlen = ieee80211_get_hdrlen(fc);
  203. if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
  204. memcmp(skb->data + hdrlen, eapol_header,
  205. sizeof(eapol_header)) == 0))
  206. return 1;
  207. return 0;
  208. }
  209. void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
  210. {
  211. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  212. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  213. if (tx->u.tx.extra_frag) {
  214. struct ieee80211_hdr *fhdr;
  215. int i;
  216. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  217. fhdr = (struct ieee80211_hdr *)
  218. tx->u.tx.extra_frag[i]->data;
  219. fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  220. }
  221. }
  222. }
  223. int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  224. int rate, int erp, int short_preamble)
  225. {
  226. int dur;
  227. /* calculate duration (in microseconds, rounded up to next higher
  228. * integer if it includes a fractional microsecond) to send frame of
  229. * len bytes (does not include FCS) at the given rate. Duration will
  230. * also include SIFS.
  231. *
  232. * rate is in 100 kbps, so divident is multiplied by 10 in the
  233. * DIV_ROUND_UP() operations.
  234. */
  235. if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
  236. local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
  237. /*
  238. * OFDM:
  239. *
  240. * N_DBPS = DATARATE x 4
  241. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  242. * (16 = SIGNAL time, 6 = tail bits)
  243. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  244. *
  245. * T_SYM = 4 usec
  246. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  247. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  248. * signal ext = 6 usec
  249. */
  250. /* FIX: Atheros Turbo may have different (shorter) duration? */
  251. dur = 16; /* SIFS + signal ext */
  252. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  253. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  254. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  255. 4 * rate); /* T_SYM x N_SYM */
  256. } else {
  257. /*
  258. * 802.11b or 802.11g with 802.11b compatibility:
  259. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  260. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  261. *
  262. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  263. * aSIFSTime = 10 usec
  264. * aPreambleLength = 144 usec or 72 usec with short preamble
  265. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  266. */
  267. dur = 10; /* aSIFSTime = 10 usec */
  268. dur += short_preamble ? (72 + 24) : (144 + 48);
  269. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  270. }
  271. return dur;
  272. }
  273. /* Exported duration function for driver use */
  274. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  275. size_t frame_len, int rate)
  276. {
  277. struct ieee80211_local *local = hw_to_local(hw);
  278. u16 dur;
  279. int erp;
  280. erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
  281. dur = ieee80211_frame_duration(local, frame_len, rate,
  282. erp, local->short_preamble);
  283. return cpu_to_le16(dur);
  284. }
  285. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  286. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  287. size_t frame_len,
  288. const struct ieee80211_tx_control *frame_txctl)
  289. {
  290. struct ieee80211_local *local = hw_to_local(hw);
  291. struct ieee80211_rate *rate;
  292. int short_preamble = local->short_preamble;
  293. int erp;
  294. u16 dur;
  295. rate = frame_txctl->rts_rate;
  296. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  297. /* CTS duration */
  298. dur = ieee80211_frame_duration(local, 10, rate->rate,
  299. erp, short_preamble);
  300. /* Data frame duration */
  301. dur += ieee80211_frame_duration(local, frame_len, rate->rate,
  302. erp, short_preamble);
  303. /* ACK duration */
  304. dur += ieee80211_frame_duration(local, 10, rate->rate,
  305. erp, short_preamble);
  306. return cpu_to_le16(dur);
  307. }
  308. EXPORT_SYMBOL(ieee80211_rts_duration);
  309. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  310. size_t frame_len,
  311. const struct ieee80211_tx_control *frame_txctl)
  312. {
  313. struct ieee80211_local *local = hw_to_local(hw);
  314. struct ieee80211_rate *rate;
  315. int short_preamble = local->short_preamble;
  316. int erp;
  317. u16 dur;
  318. rate = frame_txctl->rts_rate;
  319. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  320. /* Data frame duration */
  321. dur = ieee80211_frame_duration(local, frame_len, rate->rate,
  322. erp, short_preamble);
  323. if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
  324. /* ACK duration */
  325. dur += ieee80211_frame_duration(local, 10, rate->rate,
  326. erp, short_preamble);
  327. }
  328. return cpu_to_le16(dur);
  329. }
  330. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  331. struct ieee80211_rate *
  332. ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
  333. {
  334. struct ieee80211_hw_mode *mode;
  335. int r;
  336. list_for_each_entry(mode, &local->modes_list, list) {
  337. if (mode->mode != phymode)
  338. continue;
  339. for (r = 0; r < mode->num_rates; r++) {
  340. struct ieee80211_rate *rate = &mode->rates[r];
  341. if (rate->val == hw_rate ||
  342. (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
  343. rate->val2 == hw_rate))
  344. return rate;
  345. }
  346. }
  347. return NULL;
  348. }
  349. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  350. {
  351. struct ieee80211_local *local = hw_to_local(hw);
  352. if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
  353. &local->state[queue])) {
  354. if (test_bit(IEEE80211_LINK_STATE_PENDING,
  355. &local->state[queue]))
  356. tasklet_schedule(&local->tx_pending_tasklet);
  357. else
  358. if (!ieee80211_qdisc_installed(local->mdev)) {
  359. if (queue == 0)
  360. netif_wake_queue(local->mdev);
  361. } else
  362. __netif_schedule(local->mdev);
  363. }
  364. }
  365. EXPORT_SYMBOL(ieee80211_wake_queue);
  366. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  367. {
  368. struct ieee80211_local *local = hw_to_local(hw);
  369. if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
  370. netif_stop_queue(local->mdev);
  371. set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  372. }
  373. EXPORT_SYMBOL(ieee80211_stop_queue);
  374. void ieee80211_start_queues(struct ieee80211_hw *hw)
  375. {
  376. struct ieee80211_local *local = hw_to_local(hw);
  377. int i;
  378. for (i = 0; i < local->hw.queues; i++)
  379. clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
  380. if (!ieee80211_qdisc_installed(local->mdev))
  381. netif_start_queue(local->mdev);
  382. }
  383. EXPORT_SYMBOL(ieee80211_start_queues);
  384. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  385. {
  386. int i;
  387. for (i = 0; i < hw->queues; i++)
  388. ieee80211_stop_queue(hw, i);
  389. }
  390. EXPORT_SYMBOL(ieee80211_stop_queues);
  391. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  392. {
  393. int i;
  394. for (i = 0; i < hw->queues; i++)
  395. ieee80211_wake_queue(hw, i);
  396. }
  397. EXPORT_SYMBOL(ieee80211_wake_queues);