util.c 27 KB

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