util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. /* No encapsulation header if EtherType < 0x600 (=length) */
  38. static const unsigned char eapol_header[] =
  39. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
  40. static int rate_list_match(const int *rate_list, int rate)
  41. {
  42. int i;
  43. if (!rate_list)
  44. return 0;
  45. for (i = 0; rate_list[i] >= 0; i++)
  46. if (rate_list[i] == rate)
  47. return 1;
  48. return 0;
  49. }
  50. void ieee80211_prepare_rates(struct ieee80211_local *local,
  51. struct ieee80211_hw_mode *mode)
  52. {
  53. int i;
  54. for (i = 0; i < mode->num_rates; i++) {
  55. struct ieee80211_rate *rate = &mode->rates[i];
  56. rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
  57. IEEE80211_RATE_BASIC);
  58. if (local->supp_rates[mode->mode]) {
  59. if (!rate_list_match(local->supp_rates[mode->mode],
  60. rate->rate))
  61. continue;
  62. }
  63. rate->flags |= IEEE80211_RATE_SUPPORTED;
  64. /* Use configured basic rate set if it is available. If not,
  65. * use defaults that are sane for most cases. */
  66. if (local->basic_rates[mode->mode]) {
  67. if (rate_list_match(local->basic_rates[mode->mode],
  68. rate->rate))
  69. rate->flags |= IEEE80211_RATE_BASIC;
  70. } else switch (mode->mode) {
  71. case MODE_IEEE80211A:
  72. if (rate->rate == 60 || rate->rate == 120 ||
  73. rate->rate == 240)
  74. rate->flags |= IEEE80211_RATE_BASIC;
  75. break;
  76. case MODE_IEEE80211B:
  77. if (rate->rate == 10 || rate->rate == 20)
  78. rate->flags |= IEEE80211_RATE_BASIC;
  79. break;
  80. case MODE_IEEE80211G:
  81. if (rate->rate == 10 || rate->rate == 20 ||
  82. rate->rate == 55 || rate->rate == 110)
  83. rate->flags |= IEEE80211_RATE_BASIC;
  84. break;
  85. case NUM_IEEE80211_MODES:
  86. /* not useful */
  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_IEEE80211G:
  101. if (rate->rate == 10 || rate->rate == 20 ||
  102. rate->rate == 55 || rate->rate == 110 ||
  103. rate->rate == 60 || rate->rate == 120 ||
  104. rate->rate == 240)
  105. rate->flags |= IEEE80211_RATE_MANDATORY;
  106. break;
  107. case NUM_IEEE80211_MODES:
  108. /* not useful */
  109. break;
  110. }
  111. if (ieee80211_is_erp_rate(mode->mode, rate->rate))
  112. rate->flags |= IEEE80211_RATE_ERP;
  113. }
  114. }
  115. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
  116. {
  117. u16 fc;
  118. if (len < 24)
  119. return NULL;
  120. fc = le16_to_cpu(hdr->frame_control);
  121. switch (fc & IEEE80211_FCTL_FTYPE) {
  122. case IEEE80211_FTYPE_DATA:
  123. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  124. case IEEE80211_FCTL_TODS:
  125. return hdr->addr1;
  126. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  127. return NULL;
  128. case IEEE80211_FCTL_FROMDS:
  129. return hdr->addr2;
  130. case 0:
  131. return hdr->addr3;
  132. }
  133. break;
  134. case IEEE80211_FTYPE_MGMT:
  135. return hdr->addr3;
  136. case IEEE80211_FTYPE_CTL:
  137. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
  138. return hdr->addr1;
  139. else
  140. return NULL;
  141. }
  142. return NULL;
  143. }
  144. int ieee80211_get_hdrlen(u16 fc)
  145. {
  146. int hdrlen = 24;
  147. switch (fc & IEEE80211_FCTL_FTYPE) {
  148. case IEEE80211_FTYPE_DATA:
  149. if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
  150. hdrlen = 30; /* Addr4 */
  151. /*
  152. * The QoS Control field is two bytes and its presence is
  153. * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
  154. * hdrlen if that bit is set.
  155. * This works by masking out the bit and shifting it to
  156. * bit position 1 so the result has the value 0 or 2.
  157. */
  158. hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
  159. >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
  160. break;
  161. case IEEE80211_FTYPE_CTL:
  162. /*
  163. * ACK and CTS are 10 bytes, all others 16. To see how
  164. * to get this condition consider
  165. * subtype mask: 0b0000000011110000 (0x00F0)
  166. * ACK subtype: 0b0000000011010000 (0x00D0)
  167. * CTS subtype: 0b0000000011000000 (0x00C0)
  168. * bits that matter: ^^^ (0x00E0)
  169. * value of those: 0b0000000011000000 (0x00C0)
  170. */
  171. if ((fc & 0xE0) == 0xC0)
  172. hdrlen = 10;
  173. else
  174. hdrlen = 16;
  175. break;
  176. }
  177. return hdrlen;
  178. }
  179. EXPORT_SYMBOL(ieee80211_get_hdrlen);
  180. int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  181. {
  182. const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
  183. int hdrlen;
  184. if (unlikely(skb->len < 10))
  185. return 0;
  186. hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  187. if (unlikely(hdrlen > skb->len))
  188. return 0;
  189. return hdrlen;
  190. }
  191. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  192. int ieee80211_is_eapol(const struct sk_buff *skb)
  193. {
  194. const struct ieee80211_hdr *hdr;
  195. u16 fc;
  196. int hdrlen;
  197. if (unlikely(skb->len < 10))
  198. return 0;
  199. hdr = (const struct ieee80211_hdr *) skb->data;
  200. fc = le16_to_cpu(hdr->frame_control);
  201. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  202. return 0;
  203. hdrlen = ieee80211_get_hdrlen(fc);
  204. if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
  205. memcmp(skb->data + hdrlen, eapol_header,
  206. sizeof(eapol_header)) == 0))
  207. return 1;
  208. return 0;
  209. }
  210. void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
  211. {
  212. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  213. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  214. if (tx->u.tx.extra_frag) {
  215. struct ieee80211_hdr *fhdr;
  216. int i;
  217. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  218. fhdr = (struct ieee80211_hdr *)
  219. tx->u.tx.extra_frag[i]->data;
  220. fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  221. }
  222. }
  223. }
  224. int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  225. int rate, int erp, int short_preamble)
  226. {
  227. int dur;
  228. /* calculate duration (in microseconds, rounded up to next higher
  229. * integer if it includes a fractional microsecond) to send frame of
  230. * len bytes (does not include FCS) at the given rate. Duration will
  231. * also include SIFS.
  232. *
  233. * rate is in 100 kbps, so divident is multiplied by 10 in the
  234. * DIV_ROUND_UP() operations.
  235. */
  236. if (local->hw.conf.phymode == MODE_IEEE80211A || erp) {
  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. dur = 16; /* SIFS + signal ext */
  251. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  252. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  253. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  254. 4 * rate); /* T_SYM x N_SYM */
  255. } else {
  256. /*
  257. * 802.11b or 802.11g with 802.11b compatibility:
  258. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  259. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  260. *
  261. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  262. * aSIFSTime = 10 usec
  263. * aPreambleLength = 144 usec or 72 usec with short preamble
  264. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  265. */
  266. dur = 10; /* aSIFSTime = 10 usec */
  267. dur += short_preamble ? (72 + 24) : (144 + 48);
  268. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  269. }
  270. return dur;
  271. }
  272. /* Exported duration function for driver use */
  273. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw, int if_id,
  274. size_t frame_len, int rate)
  275. {
  276. struct ieee80211_local *local = hw_to_local(hw);
  277. struct net_device *bdev = dev_get_by_index(&init_net, if_id);
  278. struct ieee80211_sub_if_data *sdata;
  279. u16 dur;
  280. int erp;
  281. if (unlikely(!bdev))
  282. return 0;
  283. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  284. erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
  285. dur = ieee80211_frame_duration(local, frame_len, rate,
  286. erp, sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE);
  287. dev_put(bdev);
  288. return cpu_to_le16(dur);
  289. }
  290. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  291. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw, int if_id,
  292. size_t frame_len,
  293. const struct ieee80211_tx_control *frame_txctl)
  294. {
  295. struct ieee80211_local *local = hw_to_local(hw);
  296. struct ieee80211_rate *rate;
  297. struct net_device *bdev = dev_get_by_index(&init_net, if_id);
  298. struct ieee80211_sub_if_data *sdata;
  299. int short_preamble;
  300. int erp;
  301. u16 dur;
  302. if (unlikely(!bdev))
  303. return 0;
  304. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  305. short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
  306. rate = frame_txctl->rts_rate;
  307. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  308. /* CTS duration */
  309. dur = ieee80211_frame_duration(local, 10, rate->rate,
  310. erp, short_preamble);
  311. /* Data frame duration */
  312. dur += ieee80211_frame_duration(local, frame_len, rate->rate,
  313. erp, short_preamble);
  314. /* ACK duration */
  315. dur += ieee80211_frame_duration(local, 10, rate->rate,
  316. erp, short_preamble);
  317. dev_put(bdev);
  318. return cpu_to_le16(dur);
  319. }
  320. EXPORT_SYMBOL(ieee80211_rts_duration);
  321. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw, int if_id,
  322. size_t frame_len,
  323. const struct ieee80211_tx_control *frame_txctl)
  324. {
  325. struct ieee80211_local *local = hw_to_local(hw);
  326. struct ieee80211_rate *rate;
  327. struct net_device *bdev = dev_get_by_index(&init_net, if_id);
  328. struct ieee80211_sub_if_data *sdata;
  329. int short_preamble;
  330. int erp;
  331. u16 dur;
  332. if (unlikely(!bdev))
  333. return 0;
  334. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  335. short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
  336. rate = frame_txctl->rts_rate;
  337. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  338. /* Data frame duration */
  339. dur = ieee80211_frame_duration(local, frame_len, rate->rate,
  340. erp, short_preamble);
  341. if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
  342. /* ACK duration */
  343. dur += ieee80211_frame_duration(local, 10, rate->rate,
  344. erp, short_preamble);
  345. }
  346. dev_put(bdev);
  347. return cpu_to_le16(dur);
  348. }
  349. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  350. struct ieee80211_rate *
  351. ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
  352. {
  353. struct ieee80211_hw_mode *mode;
  354. int r;
  355. list_for_each_entry(mode, &local->modes_list, list) {
  356. if (mode->mode != phymode)
  357. continue;
  358. for (r = 0; r < mode->num_rates; r++) {
  359. struct ieee80211_rate *rate = &mode->rates[r];
  360. if (rate->val == hw_rate ||
  361. (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
  362. rate->val2 == hw_rate))
  363. return rate;
  364. }
  365. }
  366. return NULL;
  367. }
  368. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  369. {
  370. struct ieee80211_local *local = hw_to_local(hw);
  371. if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
  372. &local->state[queue])) {
  373. if (test_bit(IEEE80211_LINK_STATE_PENDING,
  374. &local->state[queue]))
  375. tasklet_schedule(&local->tx_pending_tasklet);
  376. else
  377. if (!ieee80211_qdisc_installed(local->mdev)) {
  378. if (queue == 0)
  379. netif_wake_queue(local->mdev);
  380. } else
  381. __netif_schedule(local->mdev);
  382. }
  383. }
  384. EXPORT_SYMBOL(ieee80211_wake_queue);
  385. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  386. {
  387. struct ieee80211_local *local = hw_to_local(hw);
  388. if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
  389. netif_stop_queue(local->mdev);
  390. set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  391. }
  392. EXPORT_SYMBOL(ieee80211_stop_queue);
  393. void ieee80211_start_queues(struct ieee80211_hw *hw)
  394. {
  395. struct ieee80211_local *local = hw_to_local(hw);
  396. int i;
  397. for (i = 0; i < local->hw.queues; i++)
  398. clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
  399. if (!ieee80211_qdisc_installed(local->mdev))
  400. netif_start_queue(local->mdev);
  401. }
  402. EXPORT_SYMBOL(ieee80211_start_queues);
  403. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  404. {
  405. int i;
  406. for (i = 0; i < hw->queues; i++)
  407. ieee80211_stop_queue(hw, i);
  408. }
  409. EXPORT_SYMBOL(ieee80211_stop_queues);
  410. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  411. {
  412. int i;
  413. for (i = 0; i < hw->queues; i++)
  414. ieee80211_wake_queue(hw, i);
  415. }
  416. EXPORT_SYMBOL(ieee80211_wake_queues);
  417. void ieee80211_iterate_active_interfaces(struct ieee80211_hw *hw,
  418. void (*iterator)(void *data, u8 *mac,
  419. int if_id),
  420. void *data)
  421. {
  422. struct ieee80211_local *local = hw_to_local(hw);
  423. struct ieee80211_sub_if_data *sdata;
  424. ASSERT_RTNL();
  425. /* we hold the RTNL here so can safely walk the list */
  426. list_for_each_entry(sdata, &local->interfaces, list) {
  427. switch (sdata->type) {
  428. case IEEE80211_IF_TYPE_INVALID:
  429. case IEEE80211_IF_TYPE_MNTR:
  430. case IEEE80211_IF_TYPE_VLAN:
  431. continue;
  432. case IEEE80211_IF_TYPE_AP:
  433. case IEEE80211_IF_TYPE_STA:
  434. case IEEE80211_IF_TYPE_IBSS:
  435. case IEEE80211_IF_TYPE_WDS:
  436. break;
  437. }
  438. if (sdata->dev == local->mdev)
  439. continue;
  440. if (netif_running(sdata->dev))
  441. iterator(data, sdata->dev->dev_addr,
  442. sdata->dev->ifindex);
  443. }
  444. }
  445. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);