util.c 26 KB

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