util.c 29 KB

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