util.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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_stop_queues_by_reason(struct ieee80211_hw *hw,
  290. enum queue_stop_reason reason)
  291. {
  292. struct ieee80211_local *local = hw_to_local(hw);
  293. unsigned long flags;
  294. int i;
  295. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  296. for (i = 0; i < hw->queues; i++)
  297. __ieee80211_stop_queue(hw, i, reason);
  298. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  299. }
  300. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  301. {
  302. ieee80211_stop_queues_by_reason(hw,
  303. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  304. }
  305. EXPORT_SYMBOL(ieee80211_stop_queues);
  306. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  307. {
  308. struct ieee80211_local *local = hw_to_local(hw);
  309. if (WARN_ON(queue >= hw->queues))
  310. return true;
  311. return __netif_subqueue_stopped(local->mdev, queue);
  312. }
  313. EXPORT_SYMBOL(ieee80211_queue_stopped);
  314. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  315. enum queue_stop_reason reason)
  316. {
  317. struct ieee80211_local *local = hw_to_local(hw);
  318. unsigned long flags;
  319. int i;
  320. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  321. for (i = 0; i < hw->queues; i++)
  322. __ieee80211_wake_queue(hw, i, reason);
  323. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  324. }
  325. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  326. {
  327. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  328. }
  329. EXPORT_SYMBOL(ieee80211_wake_queues);
  330. void ieee80211_iterate_active_interfaces(
  331. struct ieee80211_hw *hw,
  332. void (*iterator)(void *data, u8 *mac,
  333. struct ieee80211_vif *vif),
  334. void *data)
  335. {
  336. struct ieee80211_local *local = hw_to_local(hw);
  337. struct ieee80211_sub_if_data *sdata;
  338. mutex_lock(&local->iflist_mtx);
  339. list_for_each_entry(sdata, &local->interfaces, list) {
  340. switch (sdata->vif.type) {
  341. case __NL80211_IFTYPE_AFTER_LAST:
  342. case NL80211_IFTYPE_UNSPECIFIED:
  343. case NL80211_IFTYPE_MONITOR:
  344. case NL80211_IFTYPE_AP_VLAN:
  345. continue;
  346. case NL80211_IFTYPE_AP:
  347. case NL80211_IFTYPE_STATION:
  348. case NL80211_IFTYPE_ADHOC:
  349. case NL80211_IFTYPE_WDS:
  350. case NL80211_IFTYPE_MESH_POINT:
  351. break;
  352. }
  353. if (netif_running(sdata->dev))
  354. iterator(data, sdata->dev->dev_addr,
  355. &sdata->vif);
  356. }
  357. mutex_unlock(&local->iflist_mtx);
  358. }
  359. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  360. void ieee80211_iterate_active_interfaces_atomic(
  361. struct ieee80211_hw *hw,
  362. void (*iterator)(void *data, u8 *mac,
  363. struct ieee80211_vif *vif),
  364. void *data)
  365. {
  366. struct ieee80211_local *local = hw_to_local(hw);
  367. struct ieee80211_sub_if_data *sdata;
  368. rcu_read_lock();
  369. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  370. switch (sdata->vif.type) {
  371. case __NL80211_IFTYPE_AFTER_LAST:
  372. case NL80211_IFTYPE_UNSPECIFIED:
  373. case NL80211_IFTYPE_MONITOR:
  374. case NL80211_IFTYPE_AP_VLAN:
  375. continue;
  376. case NL80211_IFTYPE_AP:
  377. case NL80211_IFTYPE_STATION:
  378. case NL80211_IFTYPE_ADHOC:
  379. case NL80211_IFTYPE_WDS:
  380. case NL80211_IFTYPE_MESH_POINT:
  381. break;
  382. }
  383. if (netif_running(sdata->dev))
  384. iterator(data, sdata->dev->dev_addr,
  385. &sdata->vif);
  386. }
  387. rcu_read_unlock();
  388. }
  389. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  390. void ieee802_11_parse_elems(u8 *start, size_t len,
  391. struct ieee802_11_elems *elems)
  392. {
  393. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  394. }
  395. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  396. struct ieee802_11_elems *elems,
  397. u64 filter, u32 crc)
  398. {
  399. size_t left = len;
  400. u8 *pos = start;
  401. bool calc_crc = filter != 0;
  402. memset(elems, 0, sizeof(*elems));
  403. elems->ie_start = start;
  404. elems->total_len = len;
  405. while (left >= 2) {
  406. u8 id, elen;
  407. id = *pos++;
  408. elen = *pos++;
  409. left -= 2;
  410. if (elen > left)
  411. break;
  412. if (calc_crc && id < 64 && (filter & BIT(id)))
  413. crc = crc32_be(crc, pos - 2, elen + 2);
  414. switch (id) {
  415. case WLAN_EID_SSID:
  416. elems->ssid = pos;
  417. elems->ssid_len = elen;
  418. break;
  419. case WLAN_EID_SUPP_RATES:
  420. elems->supp_rates = pos;
  421. elems->supp_rates_len = elen;
  422. break;
  423. case WLAN_EID_FH_PARAMS:
  424. elems->fh_params = pos;
  425. elems->fh_params_len = elen;
  426. break;
  427. case WLAN_EID_DS_PARAMS:
  428. elems->ds_params = pos;
  429. elems->ds_params_len = elen;
  430. break;
  431. case WLAN_EID_CF_PARAMS:
  432. elems->cf_params = pos;
  433. elems->cf_params_len = elen;
  434. break;
  435. case WLAN_EID_TIM:
  436. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  437. elems->tim = (void *)pos;
  438. elems->tim_len = elen;
  439. }
  440. break;
  441. case WLAN_EID_IBSS_PARAMS:
  442. elems->ibss_params = pos;
  443. elems->ibss_params_len = elen;
  444. break;
  445. case WLAN_EID_CHALLENGE:
  446. elems->challenge = pos;
  447. elems->challenge_len = elen;
  448. break;
  449. case WLAN_EID_VENDOR_SPECIFIC:
  450. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  451. pos[2] == 0xf2) {
  452. /* Microsoft OUI (00:50:F2) */
  453. if (calc_crc)
  454. crc = crc32_be(crc, pos - 2, elen + 2);
  455. if (pos[3] == 1) {
  456. /* OUI Type 1 - WPA IE */
  457. elems->wpa = pos;
  458. elems->wpa_len = elen;
  459. } else if (elen >= 5 && pos[3] == 2) {
  460. /* OUI Type 2 - WMM IE */
  461. if (pos[4] == 0) {
  462. elems->wmm_info = pos;
  463. elems->wmm_info_len = elen;
  464. } else if (pos[4] == 1) {
  465. elems->wmm_param = pos;
  466. elems->wmm_param_len = elen;
  467. }
  468. }
  469. }
  470. break;
  471. case WLAN_EID_RSN:
  472. elems->rsn = pos;
  473. elems->rsn_len = elen;
  474. break;
  475. case WLAN_EID_ERP_INFO:
  476. elems->erp_info = pos;
  477. elems->erp_info_len = elen;
  478. break;
  479. case WLAN_EID_EXT_SUPP_RATES:
  480. elems->ext_supp_rates = pos;
  481. elems->ext_supp_rates_len = elen;
  482. break;
  483. case WLAN_EID_HT_CAPABILITY:
  484. if (elen >= sizeof(struct ieee80211_ht_cap))
  485. elems->ht_cap_elem = (void *)pos;
  486. break;
  487. case WLAN_EID_HT_INFORMATION:
  488. if (elen >= sizeof(struct ieee80211_ht_info))
  489. elems->ht_info_elem = (void *)pos;
  490. break;
  491. case WLAN_EID_MESH_ID:
  492. elems->mesh_id = pos;
  493. elems->mesh_id_len = elen;
  494. break;
  495. case WLAN_EID_MESH_CONFIG:
  496. elems->mesh_config = pos;
  497. elems->mesh_config_len = elen;
  498. break;
  499. case WLAN_EID_PEER_LINK:
  500. elems->peer_link = pos;
  501. elems->peer_link_len = elen;
  502. break;
  503. case WLAN_EID_PREQ:
  504. elems->preq = pos;
  505. elems->preq_len = elen;
  506. break;
  507. case WLAN_EID_PREP:
  508. elems->prep = pos;
  509. elems->prep_len = elen;
  510. break;
  511. case WLAN_EID_PERR:
  512. elems->perr = pos;
  513. elems->perr_len = elen;
  514. break;
  515. case WLAN_EID_CHANNEL_SWITCH:
  516. elems->ch_switch_elem = pos;
  517. elems->ch_switch_elem_len = elen;
  518. break;
  519. case WLAN_EID_QUIET:
  520. if (!elems->quiet_elem) {
  521. elems->quiet_elem = pos;
  522. elems->quiet_elem_len = elen;
  523. }
  524. elems->num_of_quiet_elem++;
  525. break;
  526. case WLAN_EID_COUNTRY:
  527. elems->country_elem = pos;
  528. elems->country_elem_len = elen;
  529. break;
  530. case WLAN_EID_PWR_CONSTRAINT:
  531. elems->pwr_constr_elem = pos;
  532. elems->pwr_constr_elem_len = elen;
  533. break;
  534. case WLAN_EID_TIMEOUT_INTERVAL:
  535. elems->timeout_int = pos;
  536. elems->timeout_int_len = elen;
  537. break;
  538. default:
  539. break;
  540. }
  541. left -= elen;
  542. pos += elen;
  543. }
  544. return crc;
  545. }
  546. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  547. {
  548. struct ieee80211_local *local = sdata->local;
  549. struct ieee80211_tx_queue_params qparam;
  550. int queue;
  551. bool use_11b;
  552. int aCWmin, aCWmax;
  553. if (!local->ops->conf_tx)
  554. return;
  555. memset(&qparam, 0, sizeof(qparam));
  556. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  557. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  558. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  559. /* Set defaults according to 802.11-2007 Table 7-37 */
  560. aCWmax = 1023;
  561. if (use_11b)
  562. aCWmin = 31;
  563. else
  564. aCWmin = 15;
  565. switch (queue) {
  566. case 3: /* AC_BK */
  567. qparam.cw_max = aCWmin;
  568. qparam.cw_min = aCWmax;
  569. qparam.txop = 0;
  570. qparam.aifs = 7;
  571. break;
  572. default: /* never happens but let's not leave undefined */
  573. case 2: /* AC_BE */
  574. qparam.cw_max = aCWmin;
  575. qparam.cw_min = aCWmax;
  576. qparam.txop = 0;
  577. qparam.aifs = 3;
  578. break;
  579. case 1: /* AC_VI */
  580. qparam.cw_max = aCWmin;
  581. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  582. if (use_11b)
  583. qparam.txop = 6016/32;
  584. else
  585. qparam.txop = 3008/32;
  586. qparam.aifs = 2;
  587. break;
  588. case 0: /* AC_VO */
  589. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  590. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  591. if (use_11b)
  592. qparam.txop = 3264/32;
  593. else
  594. qparam.txop = 1504/32;
  595. qparam.aifs = 2;
  596. break;
  597. }
  598. drv_conf_tx(local, queue, &qparam);
  599. }
  600. }
  601. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  602. const size_t supp_rates_len,
  603. const u8 *supp_rates)
  604. {
  605. struct ieee80211_local *local = sdata->local;
  606. int i, have_higher_than_11mbit = 0;
  607. /* cf. IEEE 802.11 9.2.12 */
  608. for (i = 0; i < supp_rates_len; i++)
  609. if ((supp_rates[i] & 0x7f) * 5 > 110)
  610. have_higher_than_11mbit = 1;
  611. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  612. have_higher_than_11mbit)
  613. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  614. else
  615. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  616. ieee80211_set_wmm_default(sdata);
  617. }
  618. void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  619. int encrypt)
  620. {
  621. skb->dev = sdata->local->mdev;
  622. skb_set_mac_header(skb, 0);
  623. skb_set_network_header(skb, 0);
  624. skb_set_transport_header(skb, 0);
  625. skb->iif = sdata->dev->ifindex;
  626. skb->do_not_encrypt = !encrypt;
  627. dev_queue_xmit(skb);
  628. }
  629. int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freqMHz)
  630. {
  631. int ret = -EINVAL;
  632. struct ieee80211_channel *chan;
  633. struct ieee80211_local *local = sdata->local;
  634. chan = ieee80211_get_channel(local->hw.wiphy, freqMHz);
  635. if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
  636. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  637. chan->flags & IEEE80211_CHAN_NO_IBSS)
  638. return ret;
  639. local->oper_channel = chan;
  640. local->oper_channel_type = NL80211_CHAN_NO_HT;
  641. if (local->sw_scanning || local->hw_scanning)
  642. ret = 0;
  643. else
  644. ret = ieee80211_hw_config(
  645. local, IEEE80211_CONF_CHANGE_CHANNEL);
  646. }
  647. return ret;
  648. }
  649. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  650. enum ieee80211_band band)
  651. {
  652. struct ieee80211_supported_band *sband;
  653. struct ieee80211_rate *bitrates;
  654. u32 mandatory_rates;
  655. enum ieee80211_rate_flags mandatory_flag;
  656. int i;
  657. sband = local->hw.wiphy->bands[band];
  658. if (!sband) {
  659. WARN_ON(1);
  660. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  661. }
  662. if (band == IEEE80211_BAND_2GHZ)
  663. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  664. else
  665. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  666. bitrates = sband->bitrates;
  667. mandatory_rates = 0;
  668. for (i = 0; i < sband->n_bitrates; i++)
  669. if (bitrates[i].flags & mandatory_flag)
  670. mandatory_rates |= BIT(i);
  671. return mandatory_rates;
  672. }
  673. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  674. u16 transaction, u16 auth_alg,
  675. u8 *extra, size_t extra_len,
  676. const u8 *bssid, int encrypt)
  677. {
  678. struct ieee80211_local *local = sdata->local;
  679. struct sk_buff *skb;
  680. struct ieee80211_mgmt *mgmt;
  681. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  682. sizeof(*mgmt) + 6 + extra_len);
  683. if (!skb) {
  684. printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
  685. "frame\n", sdata->dev->name);
  686. return;
  687. }
  688. skb_reserve(skb, local->hw.extra_tx_headroom);
  689. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  690. memset(mgmt, 0, 24 + 6);
  691. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  692. IEEE80211_STYPE_AUTH);
  693. if (encrypt)
  694. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  695. memcpy(mgmt->da, bssid, ETH_ALEN);
  696. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  697. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  698. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  699. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  700. mgmt->u.auth.status_code = cpu_to_le16(0);
  701. if (extra)
  702. memcpy(skb_put(skb, extra_len), extra, extra_len);
  703. ieee80211_tx_skb(sdata, skb, encrypt);
  704. }
  705. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  706. const u8 *ie, size_t ie_len)
  707. {
  708. struct ieee80211_supported_band *sband;
  709. u8 *pos, *supp_rates_len, *esupp_rates_len = NULL;
  710. int i;
  711. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  712. pos = buffer;
  713. *pos++ = WLAN_EID_SUPP_RATES;
  714. supp_rates_len = pos;
  715. *pos++ = 0;
  716. for (i = 0; i < sband->n_bitrates; i++) {
  717. struct ieee80211_rate *rate = &sband->bitrates[i];
  718. if (esupp_rates_len) {
  719. *esupp_rates_len += 1;
  720. } else if (*supp_rates_len == 8) {
  721. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  722. esupp_rates_len = pos;
  723. *pos++ = 1;
  724. } else
  725. *supp_rates_len += 1;
  726. *pos++ = rate->bitrate / 5;
  727. }
  728. if (sband->ht_cap.ht_supported) {
  729. __le16 tmp = cpu_to_le16(sband->ht_cap.cap);
  730. *pos++ = WLAN_EID_HT_CAPABILITY;
  731. *pos++ = sizeof(struct ieee80211_ht_cap);
  732. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  733. memcpy(pos, &tmp, sizeof(u16));
  734. pos += sizeof(u16);
  735. /* TODO: needs a define here for << 2 */
  736. *pos++ = sband->ht_cap.ampdu_factor |
  737. (sband->ht_cap.ampdu_density << 2);
  738. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  739. pos += sizeof(sband->ht_cap.mcs);
  740. pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
  741. }
  742. /*
  743. * If adding more here, adjust code in main.c
  744. * that calculates local->scan_ies_len.
  745. */
  746. if (ie) {
  747. memcpy(pos, ie, ie_len);
  748. pos += ie_len;
  749. }
  750. return pos - buffer;
  751. }
  752. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  753. const u8 *ssid, size_t ssid_len,
  754. const u8 *ie, size_t ie_len)
  755. {
  756. struct ieee80211_local *local = sdata->local;
  757. struct sk_buff *skb;
  758. struct ieee80211_mgmt *mgmt;
  759. u8 *pos;
  760. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
  761. ie_len);
  762. if (!skb) {
  763. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  764. "request\n", sdata->dev->name);
  765. return;
  766. }
  767. skb_reserve(skb, local->hw.extra_tx_headroom);
  768. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  769. memset(mgmt, 0, 24);
  770. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  771. IEEE80211_STYPE_PROBE_REQ);
  772. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  773. if (dst) {
  774. memcpy(mgmt->da, dst, ETH_ALEN);
  775. memcpy(mgmt->bssid, dst, ETH_ALEN);
  776. } else {
  777. memset(mgmt->da, 0xff, ETH_ALEN);
  778. memset(mgmt->bssid, 0xff, ETH_ALEN);
  779. }
  780. pos = skb_put(skb, 2 + ssid_len);
  781. *pos++ = WLAN_EID_SSID;
  782. *pos++ = ssid_len;
  783. memcpy(pos, ssid, ssid_len);
  784. pos += ssid_len;
  785. skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len));
  786. ieee80211_tx_skb(sdata, skb, 0);
  787. }
  788. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  789. struct ieee802_11_elems *elems,
  790. enum ieee80211_band band)
  791. {
  792. struct ieee80211_supported_band *sband;
  793. struct ieee80211_rate *bitrates;
  794. size_t num_rates;
  795. u32 supp_rates;
  796. int i, j;
  797. sband = local->hw.wiphy->bands[band];
  798. if (!sband) {
  799. WARN_ON(1);
  800. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  801. }
  802. bitrates = sband->bitrates;
  803. num_rates = sband->n_bitrates;
  804. supp_rates = 0;
  805. for (i = 0; i < elems->supp_rates_len +
  806. elems->ext_supp_rates_len; i++) {
  807. u8 rate = 0;
  808. int own_rate;
  809. if (i < elems->supp_rates_len)
  810. rate = elems->supp_rates[i];
  811. else if (elems->ext_supp_rates)
  812. rate = elems->ext_supp_rates
  813. [i - elems->supp_rates_len];
  814. own_rate = 5 * (rate & 0x7f);
  815. for (j = 0; j < num_rates; j++)
  816. if (bitrates[j].bitrate == own_rate)
  817. supp_rates |= BIT(j);
  818. }
  819. return supp_rates;
  820. }
  821. int ieee80211_reconfig(struct ieee80211_local *local)
  822. {
  823. struct ieee80211_hw *hw = &local->hw;
  824. struct ieee80211_sub_if_data *sdata;
  825. struct ieee80211_if_init_conf conf;
  826. struct sta_info *sta;
  827. unsigned long flags;
  828. int res;
  829. bool from_suspend = local->suspended;
  830. /*
  831. * We're going to start the hardware, at that point
  832. * we are no longer suspended and can RX frames.
  833. */
  834. local->suspended = false;
  835. /* restart hardware */
  836. if (local->open_count) {
  837. res = drv_start(local);
  838. ieee80211_led_radio(local, hw->conf.radio_enabled);
  839. }
  840. /* add interfaces */
  841. list_for_each_entry(sdata, &local->interfaces, list) {
  842. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  843. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  844. netif_running(sdata->dev)) {
  845. conf.vif = &sdata->vif;
  846. conf.type = sdata->vif.type;
  847. conf.mac_addr = sdata->dev->dev_addr;
  848. res = drv_add_interface(local, &conf);
  849. }
  850. }
  851. /* add STAs back */
  852. if (local->ops->sta_notify) {
  853. spin_lock_irqsave(&local->sta_lock, flags);
  854. list_for_each_entry(sta, &local->sta_list, list) {
  855. sdata = sta->sdata;
  856. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  857. sdata = container_of(sdata->bss,
  858. struct ieee80211_sub_if_data,
  859. u.ap);
  860. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD,
  861. &sta->sta);
  862. }
  863. spin_unlock_irqrestore(&local->sta_lock, flags);
  864. }
  865. /* Clear Suspend state so that ADDBA requests can be processed */
  866. rcu_read_lock();
  867. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  868. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  869. clear_sta_flags(sta, WLAN_STA_SUSPEND);
  870. }
  871. }
  872. rcu_read_unlock();
  873. /* setup RTS threshold */
  874. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  875. /* reconfigure hardware */
  876. ieee80211_hw_config(local, ~0);
  877. netif_addr_lock_bh(local->mdev);
  878. ieee80211_configure_filter(local);
  879. netif_addr_unlock_bh(local->mdev);
  880. /* Finally also reconfigure all the BSS information */
  881. list_for_each_entry(sdata, &local->interfaces, list) {
  882. u32 changed = ~0;
  883. if (!netif_running(sdata->dev))
  884. continue;
  885. switch (sdata->vif.type) {
  886. case NL80211_IFTYPE_STATION:
  887. /* disable beacon change bits */
  888. changed &= ~(BSS_CHANGED_BEACON |
  889. BSS_CHANGED_BEACON_ENABLED);
  890. /* fall through */
  891. case NL80211_IFTYPE_ADHOC:
  892. case NL80211_IFTYPE_AP:
  893. case NL80211_IFTYPE_MESH_POINT:
  894. ieee80211_bss_info_change_notify(sdata, changed);
  895. break;
  896. case NL80211_IFTYPE_WDS:
  897. break;
  898. case NL80211_IFTYPE_AP_VLAN:
  899. case NL80211_IFTYPE_MONITOR:
  900. /* ignore virtual */
  901. break;
  902. case NL80211_IFTYPE_UNSPECIFIED:
  903. case __NL80211_IFTYPE_AFTER_LAST:
  904. WARN_ON(1);
  905. break;
  906. }
  907. }
  908. /* add back keys */
  909. list_for_each_entry(sdata, &local->interfaces, list)
  910. if (netif_running(sdata->dev))
  911. ieee80211_enable_keys(sdata);
  912. ieee80211_wake_queues_by_reason(hw,
  913. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  914. /*
  915. * If this is for hw restart things are still running.
  916. * We may want to change that later, however.
  917. */
  918. if (!from_suspend)
  919. return 0;
  920. #ifdef CONFIG_PM
  921. local->suspended = false;
  922. list_for_each_entry(sdata, &local->interfaces, list) {
  923. switch(sdata->vif.type) {
  924. case NL80211_IFTYPE_STATION:
  925. ieee80211_sta_restart(sdata);
  926. break;
  927. case NL80211_IFTYPE_ADHOC:
  928. ieee80211_ibss_restart(sdata);
  929. break;
  930. case NL80211_IFTYPE_MESH_POINT:
  931. ieee80211_mesh_restart(sdata);
  932. break;
  933. default:
  934. break;
  935. }
  936. }
  937. add_timer(&local->sta_cleanup);
  938. spin_lock_irqsave(&local->sta_lock, flags);
  939. list_for_each_entry(sta, &local->sta_list, list)
  940. mesh_plink_restart(sta);
  941. spin_unlock_irqrestore(&local->sta_lock, flags);
  942. #else
  943. WARN_ON(1);
  944. #endif
  945. return 0;
  946. }