util.c 28 KB

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