util.c 12 KB

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