util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/net_namespace.h>
  23. #include <net/cfg80211.h>
  24. #include <net/rtnetlink.h>
  25. #include "ieee80211_i.h"
  26. #include "ieee80211_rate.h"
  27. #include "wme.h"
  28. /* privid for wiphys to determine whether they belong to us or not */
  29. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  30. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  31. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  32. const unsigned char rfc1042_header[] =
  33. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  34. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  35. const unsigned char bridge_tunnel_header[] =
  36. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  37. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  38. enum ieee80211_if_types type)
  39. {
  40. u16 fc;
  41. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  42. if (len < 16)
  43. return NULL;
  44. fc = le16_to_cpu(hdr->frame_control);
  45. switch (fc & IEEE80211_FCTL_FTYPE) {
  46. case IEEE80211_FTYPE_DATA:
  47. if (len < 24) /* drop incorrect hdr len (data) */
  48. return NULL;
  49. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  50. case IEEE80211_FCTL_TODS:
  51. return hdr->addr1;
  52. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  53. return NULL;
  54. case IEEE80211_FCTL_FROMDS:
  55. return hdr->addr2;
  56. case 0:
  57. return hdr->addr3;
  58. }
  59. break;
  60. case IEEE80211_FTYPE_MGMT:
  61. if (len < 24) /* drop incorrect hdr len (mgmt) */
  62. return NULL;
  63. return hdr->addr3;
  64. case IEEE80211_FTYPE_CTL:
  65. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
  66. return hdr->addr1;
  67. else if ((fc & IEEE80211_FCTL_STYPE) ==
  68. IEEE80211_STYPE_BACK_REQ) {
  69. switch (type) {
  70. case IEEE80211_IF_TYPE_STA:
  71. return hdr->addr2;
  72. case IEEE80211_IF_TYPE_AP:
  73. case IEEE80211_IF_TYPE_VLAN:
  74. return hdr->addr1;
  75. default:
  76. return NULL;
  77. }
  78. }
  79. else
  80. return NULL;
  81. }
  82. return NULL;
  83. }
  84. int ieee80211_get_hdrlen(u16 fc)
  85. {
  86. int hdrlen = 24;
  87. switch (fc & IEEE80211_FCTL_FTYPE) {
  88. case IEEE80211_FTYPE_DATA:
  89. if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
  90. hdrlen = 30; /* Addr4 */
  91. /*
  92. * The QoS Control field is two bytes and its presence is
  93. * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
  94. * hdrlen if that bit is set.
  95. * This works by masking out the bit and shifting it to
  96. * bit position 1 so the result has the value 0 or 2.
  97. */
  98. hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
  99. >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
  100. break;
  101. case IEEE80211_FTYPE_CTL:
  102. /*
  103. * ACK and CTS are 10 bytes, all others 16. To see how
  104. * to get this condition consider
  105. * subtype mask: 0b0000000011110000 (0x00F0)
  106. * ACK subtype: 0b0000000011010000 (0x00D0)
  107. * CTS subtype: 0b0000000011000000 (0x00C0)
  108. * bits that matter: ^^^ (0x00E0)
  109. * value of those: 0b0000000011000000 (0x00C0)
  110. */
  111. if ((fc & 0xE0) == 0xC0)
  112. hdrlen = 10;
  113. else
  114. hdrlen = 16;
  115. break;
  116. }
  117. return hdrlen;
  118. }
  119. EXPORT_SYMBOL(ieee80211_get_hdrlen);
  120. int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  121. {
  122. const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
  123. int hdrlen;
  124. if (unlikely(skb->len < 10))
  125. return 0;
  126. hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  127. if (unlikely(hdrlen > skb->len))
  128. return 0;
  129. return hdrlen;
  130. }
  131. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  132. void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
  133. {
  134. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  135. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  136. if (tx->u.tx.extra_frag) {
  137. struct ieee80211_hdr *fhdr;
  138. int i;
  139. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  140. fhdr = (struct ieee80211_hdr *)
  141. tx->u.tx.extra_frag[i]->data;
  142. fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  143. }
  144. }
  145. }
  146. int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  147. int rate, int erp, int short_preamble)
  148. {
  149. int dur;
  150. /* calculate duration (in microseconds, rounded up to next higher
  151. * integer if it includes a fractional microsecond) to send frame of
  152. * len bytes (does not include FCS) at the given rate. Duration will
  153. * also include SIFS.
  154. *
  155. * rate is in 100 kbps, so divident is multiplied by 10 in the
  156. * DIV_ROUND_UP() operations.
  157. */
  158. if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
  159. /*
  160. * OFDM:
  161. *
  162. * N_DBPS = DATARATE x 4
  163. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  164. * (16 = SIGNAL time, 6 = tail bits)
  165. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  166. *
  167. * T_SYM = 4 usec
  168. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  169. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  170. * signal ext = 6 usec
  171. */
  172. dur = 16; /* SIFS + signal ext */
  173. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  174. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  175. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  176. 4 * rate); /* T_SYM x N_SYM */
  177. } else {
  178. /*
  179. * 802.11b or 802.11g with 802.11b compatibility:
  180. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  181. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  182. *
  183. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  184. * aSIFSTime = 10 usec
  185. * aPreambleLength = 144 usec or 72 usec with short preamble
  186. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  187. */
  188. dur = 10; /* aSIFSTime = 10 usec */
  189. dur += short_preamble ? (72 + 24) : (144 + 48);
  190. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  191. }
  192. return dur;
  193. }
  194. /* Exported duration function for driver use */
  195. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  196. struct ieee80211_vif *vif,
  197. size_t frame_len,
  198. struct ieee80211_rate *rate)
  199. {
  200. struct ieee80211_local *local = hw_to_local(hw);
  201. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  202. u16 dur;
  203. int erp;
  204. erp = 0;
  205. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  206. erp = rate->flags & IEEE80211_RATE_ERP_G;
  207. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
  208. sdata->bss_conf.use_short_preamble);
  209. return cpu_to_le16(dur);
  210. }
  211. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  212. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  213. struct ieee80211_vif *vif, size_t frame_len,
  214. const struct ieee80211_tx_control *frame_txctl)
  215. {
  216. struct ieee80211_local *local = hw_to_local(hw);
  217. struct ieee80211_rate *rate;
  218. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  219. bool short_preamble;
  220. int erp;
  221. u16 dur;
  222. short_preamble = sdata->bss_conf.use_short_preamble;
  223. rate = frame_txctl->rts_cts_rate;
  224. erp = 0;
  225. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  226. erp = rate->flags & IEEE80211_RATE_ERP_G;
  227. /* CTS duration */
  228. dur = ieee80211_frame_duration(local, 10, rate->bitrate,
  229. erp, short_preamble);
  230. /* Data frame duration */
  231. dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
  232. erp, short_preamble);
  233. /* ACK duration */
  234. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  235. erp, short_preamble);
  236. return cpu_to_le16(dur);
  237. }
  238. EXPORT_SYMBOL(ieee80211_rts_duration);
  239. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  240. struct ieee80211_vif *vif,
  241. size_t frame_len,
  242. const struct ieee80211_tx_control *frame_txctl)
  243. {
  244. struct ieee80211_local *local = hw_to_local(hw);
  245. struct ieee80211_rate *rate;
  246. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  247. bool short_preamble;
  248. int erp;
  249. u16 dur;
  250. short_preamble = sdata->bss_conf.use_short_preamble;
  251. rate = frame_txctl->rts_cts_rate;
  252. erp = 0;
  253. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  254. erp = rate->flags & IEEE80211_RATE_ERP_G;
  255. /* Data frame duration */
  256. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
  257. erp, short_preamble);
  258. if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
  259. /* ACK duration */
  260. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  261. erp, short_preamble);
  262. }
  263. return cpu_to_le16(dur);
  264. }
  265. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  266. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  267. {
  268. struct ieee80211_local *local = hw_to_local(hw);
  269. if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
  270. &local->state[queue])) {
  271. if (test_bit(IEEE80211_LINK_STATE_PENDING,
  272. &local->state[queue]))
  273. tasklet_schedule(&local->tx_pending_tasklet);
  274. else
  275. if (!ieee80211_qdisc_installed(local->mdev)) {
  276. if (queue == 0)
  277. netif_wake_queue(local->mdev);
  278. } else
  279. __netif_schedule(local->mdev);
  280. }
  281. }
  282. EXPORT_SYMBOL(ieee80211_wake_queue);
  283. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  284. {
  285. struct ieee80211_local *local = hw_to_local(hw);
  286. if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
  287. netif_stop_queue(local->mdev);
  288. set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  289. }
  290. EXPORT_SYMBOL(ieee80211_stop_queue);
  291. void ieee80211_start_queues(struct ieee80211_hw *hw)
  292. {
  293. struct ieee80211_local *local = hw_to_local(hw);
  294. int i;
  295. for (i = 0; i < local->hw.queues; i++)
  296. clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
  297. if (!ieee80211_qdisc_installed(local->mdev))
  298. netif_start_queue(local->mdev);
  299. }
  300. EXPORT_SYMBOL(ieee80211_start_queues);
  301. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  302. {
  303. int i;
  304. for (i = 0; i < hw->queues; i++)
  305. ieee80211_stop_queue(hw, i);
  306. }
  307. EXPORT_SYMBOL(ieee80211_stop_queues);
  308. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  309. {
  310. int i;
  311. for (i = 0; i < hw->queues; i++)
  312. ieee80211_wake_queue(hw, i);
  313. }
  314. EXPORT_SYMBOL(ieee80211_wake_queues);
  315. void ieee80211_iterate_active_interfaces(
  316. struct ieee80211_hw *hw,
  317. void (*iterator)(void *data, u8 *mac,
  318. struct ieee80211_vif *vif),
  319. void *data)
  320. {
  321. struct ieee80211_local *local = hw_to_local(hw);
  322. struct ieee80211_sub_if_data *sdata;
  323. rcu_read_lock();
  324. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  325. switch (sdata->vif.type) {
  326. case IEEE80211_IF_TYPE_INVALID:
  327. case IEEE80211_IF_TYPE_MNTR:
  328. case IEEE80211_IF_TYPE_VLAN:
  329. continue;
  330. case IEEE80211_IF_TYPE_AP:
  331. case IEEE80211_IF_TYPE_STA:
  332. case IEEE80211_IF_TYPE_IBSS:
  333. case IEEE80211_IF_TYPE_WDS:
  334. case IEEE80211_IF_TYPE_MESH_POINT:
  335. break;
  336. }
  337. if (sdata->dev == local->mdev)
  338. continue;
  339. if (netif_running(sdata->dev))
  340. iterator(data, sdata->dev->dev_addr,
  341. &sdata->vif);
  342. }
  343. rcu_read_unlock();
  344. }
  345. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);