util.c 30 KB

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