main.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.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/rtnetlink.h>
  22. #include <linux/bitmap.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include "ieee80211_i.h"
  26. #include "rate.h"
  27. #include "mesh.h"
  28. #include "wep.h"
  29. #include "wme.h"
  30. #include "aes_ccm.h"
  31. #include "led.h"
  32. #include "cfg.h"
  33. #include "debugfs.h"
  34. #include "debugfs_netdev.h"
  35. /*
  36. * For seeing transmitted packets on monitor interfaces
  37. * we have a radiotap header too.
  38. */
  39. struct ieee80211_tx_status_rtap_hdr {
  40. struct ieee80211_radiotap_header hdr;
  41. __le16 tx_flags;
  42. u8 data_retries;
  43. } __attribute__ ((packed));
  44. /* must be called under mdev tx lock */
  45. void ieee80211_configure_filter(struct ieee80211_local *local)
  46. {
  47. unsigned int changed_flags;
  48. unsigned int new_flags = 0;
  49. if (atomic_read(&local->iff_promiscs))
  50. new_flags |= FIF_PROMISC_IN_BSS;
  51. if (atomic_read(&local->iff_allmultis))
  52. new_flags |= FIF_ALLMULTI;
  53. if (local->monitors)
  54. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  55. if (local->fif_fcsfail)
  56. new_flags |= FIF_FCSFAIL;
  57. if (local->fif_plcpfail)
  58. new_flags |= FIF_PLCPFAIL;
  59. if (local->fif_control)
  60. new_flags |= FIF_CONTROL;
  61. if (local->fif_other_bss)
  62. new_flags |= FIF_OTHER_BSS;
  63. changed_flags = local->filter_flags ^ new_flags;
  64. /* be a bit nasty */
  65. new_flags |= (1<<31);
  66. local->ops->configure_filter(local_to_hw(local),
  67. changed_flags, &new_flags,
  68. local->mdev->mc_count,
  69. local->mdev->mc_list);
  70. WARN_ON(new_flags & (1<<31));
  71. local->filter_flags = new_flags & ~(1<<31);
  72. }
  73. /* master interface */
  74. static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
  75. {
  76. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  77. return ETH_ALEN;
  78. }
  79. static const struct header_ops ieee80211_header_ops = {
  80. .create = eth_header,
  81. .parse = header_parse_80211,
  82. .rebuild = eth_rebuild_header,
  83. .cache = eth_header_cache,
  84. .cache_update = eth_header_cache_update,
  85. };
  86. static int ieee80211_master_open(struct net_device *dev)
  87. {
  88. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  89. struct ieee80211_local *local = mpriv->local;
  90. struct ieee80211_sub_if_data *sdata;
  91. int res = -EOPNOTSUPP;
  92. /* we hold the RTNL here so can safely walk the list */
  93. list_for_each_entry(sdata, &local->interfaces, list) {
  94. if (netif_running(sdata->dev)) {
  95. res = 0;
  96. break;
  97. }
  98. }
  99. if (res)
  100. return res;
  101. netif_tx_start_all_queues(local->mdev);
  102. return 0;
  103. }
  104. static int ieee80211_master_stop(struct net_device *dev)
  105. {
  106. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  107. struct ieee80211_local *local = mpriv->local;
  108. struct ieee80211_sub_if_data *sdata;
  109. /* we hold the RTNL here so can safely walk the list */
  110. list_for_each_entry(sdata, &local->interfaces, list)
  111. if (netif_running(sdata->dev))
  112. dev_close(sdata->dev);
  113. return 0;
  114. }
  115. static void ieee80211_master_set_multicast_list(struct net_device *dev)
  116. {
  117. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  118. struct ieee80211_local *local = mpriv->local;
  119. ieee80211_configure_filter(local);
  120. }
  121. /* everything else */
  122. int ieee80211_if_config(struct ieee80211_sub_if_data *sdata, u32 changed)
  123. {
  124. struct ieee80211_local *local = sdata->local;
  125. struct ieee80211_if_conf conf;
  126. if (WARN_ON(!netif_running(sdata->dev)))
  127. return 0;
  128. if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  129. return -EINVAL;
  130. if (!local->ops->config_interface)
  131. return 0;
  132. memset(&conf, 0, sizeof(conf));
  133. conf.changed = changed;
  134. if (sdata->vif.type == NL80211_IFTYPE_STATION ||
  135. sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  136. conf.bssid = sdata->u.sta.bssid;
  137. conf.ssid = sdata->u.sta.ssid;
  138. conf.ssid_len = sdata->u.sta.ssid_len;
  139. } else if (sdata->vif.type == NL80211_IFTYPE_AP) {
  140. conf.bssid = sdata->dev->dev_addr;
  141. conf.ssid = sdata->u.ap.ssid;
  142. conf.ssid_len = sdata->u.ap.ssid_len;
  143. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  144. u8 zero[ETH_ALEN] = { 0 };
  145. conf.bssid = zero;
  146. conf.ssid = zero;
  147. conf.ssid_len = 0;
  148. } else {
  149. WARN_ON(1);
  150. return -EINVAL;
  151. }
  152. if (WARN_ON(!conf.bssid && (changed & IEEE80211_IFCC_BSSID)))
  153. return -EINVAL;
  154. if (WARN_ON(!conf.ssid && (changed & IEEE80211_IFCC_SSID)))
  155. return -EINVAL;
  156. return local->ops->config_interface(local_to_hw(local),
  157. &sdata->vif, &conf);
  158. }
  159. int ieee80211_hw_config(struct ieee80211_local *local)
  160. {
  161. struct ieee80211_channel *chan;
  162. int ret = 0;
  163. if (local->sw_scanning)
  164. chan = local->scan_channel;
  165. else
  166. chan = local->oper_channel;
  167. local->hw.conf.channel = chan;
  168. if (!local->hw.conf.power_level)
  169. local->hw.conf.power_level = chan->max_power;
  170. else
  171. local->hw.conf.power_level = min(chan->max_power,
  172. local->hw.conf.power_level);
  173. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  174. printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
  175. wiphy_name(local->hw.wiphy), chan->center_freq);
  176. #endif
  177. if (local->open_count) {
  178. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  179. /*
  180. * HW reconfiguration should never fail, the driver has told
  181. * us what it can support so it should live up to that promise.
  182. */
  183. WARN_ON(ret);
  184. }
  185. return ret;
  186. }
  187. /**
  188. * ieee80211_handle_ht should be used only after legacy configuration
  189. * has been determined namely band, as ht configuration depends upon
  190. * the hardware's HT abilities for a _specific_ band.
  191. */
  192. u32 ieee80211_handle_ht(struct ieee80211_local *local, int enable_ht,
  193. struct ieee80211_ht_info *req_ht_cap,
  194. struct ieee80211_ht_bss_info *req_bss_cap)
  195. {
  196. struct ieee80211_conf *conf = &local->hw.conf;
  197. struct ieee80211_supported_band *sband;
  198. struct ieee80211_ht_info ht_conf;
  199. struct ieee80211_ht_bss_info ht_bss_conf;
  200. u32 changed = 0;
  201. int i;
  202. u8 max_tx_streams = IEEE80211_HT_CAP_MAX_STREAMS;
  203. u8 tx_mcs_set_cap;
  204. sband = local->hw.wiphy->bands[conf->channel->band];
  205. memset(&ht_conf, 0, sizeof(struct ieee80211_ht_info));
  206. memset(&ht_bss_conf, 0, sizeof(struct ieee80211_ht_bss_info));
  207. /* HT is not supported */
  208. if (!sband->ht_info.ht_supported) {
  209. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  210. goto out;
  211. }
  212. /* disable HT */
  213. if (!enable_ht) {
  214. if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)
  215. changed |= BSS_CHANGED_HT;
  216. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  217. conf->ht_conf.ht_supported = 0;
  218. goto out;
  219. }
  220. if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE))
  221. changed |= BSS_CHANGED_HT;
  222. conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
  223. ht_conf.ht_supported = 1;
  224. ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
  225. ht_conf.cap &= ~(IEEE80211_HT_CAP_SM_PS);
  226. ht_conf.cap |= sband->ht_info.cap & IEEE80211_HT_CAP_SM_PS;
  227. ht_bss_conf.primary_channel = req_bss_cap->primary_channel;
  228. ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
  229. ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
  230. ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
  231. ht_conf.ampdu_density = req_ht_cap->ampdu_density;
  232. /* Bits 96-100 */
  233. tx_mcs_set_cap = sband->ht_info.supp_mcs_set[12];
  234. /* configure suppoerted Tx MCS according to requested MCS
  235. * (based in most cases on Rx capabilities of peer) and self
  236. * Tx MCS capabilities (as defined by low level driver HW
  237. * Tx capabilities) */
  238. if (!(tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_DEFINED))
  239. goto check_changed;
  240. /* Counting from 0 therfore + 1 */
  241. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_RX_DIFF)
  242. max_tx_streams = ((tx_mcs_set_cap &
  243. IEEE80211_HT_CAP_MCS_TX_STREAMS) >> 2) + 1;
  244. for (i = 0; i < max_tx_streams; i++)
  245. ht_conf.supp_mcs_set[i] =
  246. sband->ht_info.supp_mcs_set[i] &
  247. req_ht_cap->supp_mcs_set[i];
  248. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_UEQM)
  249. for (i = IEEE80211_SUPP_MCS_SET_UEQM;
  250. i < IEEE80211_SUPP_MCS_SET_LEN; i++)
  251. ht_conf.supp_mcs_set[i] =
  252. sband->ht_info.supp_mcs_set[i] &
  253. req_ht_cap->supp_mcs_set[i];
  254. check_changed:
  255. /* if bss configuration changed store the new one */
  256. if (memcmp(&conf->ht_conf, &ht_conf, sizeof(ht_conf)) ||
  257. memcmp(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf))) {
  258. changed |= BSS_CHANGED_HT;
  259. memcpy(&conf->ht_conf, &ht_conf, sizeof(ht_conf));
  260. memcpy(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf));
  261. }
  262. out:
  263. return changed;
  264. }
  265. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  266. u32 changed)
  267. {
  268. struct ieee80211_local *local = sdata->local;
  269. if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  270. return;
  271. if (!changed)
  272. return;
  273. if (local->ops->bss_info_changed)
  274. local->ops->bss_info_changed(local_to_hw(local),
  275. &sdata->vif,
  276. &sdata->bss_conf,
  277. changed);
  278. }
  279. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  280. {
  281. sdata->bss_conf.use_cts_prot = false;
  282. sdata->bss_conf.use_short_preamble = false;
  283. sdata->bss_conf.use_short_slot = false;
  284. return BSS_CHANGED_ERP_CTS_PROT |
  285. BSS_CHANGED_ERP_PREAMBLE |
  286. BSS_CHANGED_ERP_SLOT;
  287. }
  288. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  289. struct sk_buff *skb)
  290. {
  291. struct ieee80211_local *local = hw_to_local(hw);
  292. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  293. int tmp;
  294. skb->dev = local->mdev;
  295. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  296. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  297. &local->skb_queue : &local->skb_queue_unreliable, skb);
  298. tmp = skb_queue_len(&local->skb_queue) +
  299. skb_queue_len(&local->skb_queue_unreliable);
  300. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  301. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  302. dev_kfree_skb_irq(skb);
  303. tmp--;
  304. I802_DEBUG_INC(local->tx_status_drop);
  305. }
  306. tasklet_schedule(&local->tasklet);
  307. }
  308. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  309. static void ieee80211_tasklet_handler(unsigned long data)
  310. {
  311. struct ieee80211_local *local = (struct ieee80211_local *) data;
  312. struct sk_buff *skb;
  313. struct ieee80211_rx_status rx_status;
  314. struct ieee80211_ra_tid *ra_tid;
  315. while ((skb = skb_dequeue(&local->skb_queue)) ||
  316. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  317. switch (skb->pkt_type) {
  318. case IEEE80211_RX_MSG:
  319. /* status is in skb->cb */
  320. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  321. /* Clear skb->pkt_type in order to not confuse kernel
  322. * netstack. */
  323. skb->pkt_type = 0;
  324. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  325. break;
  326. case IEEE80211_TX_STATUS_MSG:
  327. skb->pkt_type = 0;
  328. ieee80211_tx_status(local_to_hw(local), skb);
  329. break;
  330. case IEEE80211_DELBA_MSG:
  331. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  332. ieee80211_stop_tx_ba_cb(local_to_hw(local),
  333. ra_tid->ra, ra_tid->tid);
  334. dev_kfree_skb(skb);
  335. break;
  336. case IEEE80211_ADDBA_MSG:
  337. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  338. ieee80211_start_tx_ba_cb(local_to_hw(local),
  339. ra_tid->ra, ra_tid->tid);
  340. dev_kfree_skb(skb);
  341. break ;
  342. default:
  343. WARN_ON(1);
  344. dev_kfree_skb(skb);
  345. break;
  346. }
  347. }
  348. }
  349. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  350. * make a prepared TX frame (one that has been given to hw) to look like brand
  351. * new IEEE 802.11 frame that is ready to go through TX processing again.
  352. */
  353. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  354. struct ieee80211_key *key,
  355. struct sk_buff *skb)
  356. {
  357. unsigned int hdrlen, iv_len, mic_len;
  358. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  359. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  360. if (!key)
  361. goto no_key;
  362. switch (key->conf.alg) {
  363. case ALG_WEP:
  364. iv_len = WEP_IV_LEN;
  365. mic_len = WEP_ICV_LEN;
  366. break;
  367. case ALG_TKIP:
  368. iv_len = TKIP_IV_LEN;
  369. mic_len = TKIP_ICV_LEN;
  370. break;
  371. case ALG_CCMP:
  372. iv_len = CCMP_HDR_LEN;
  373. mic_len = CCMP_MIC_LEN;
  374. break;
  375. default:
  376. goto no_key;
  377. }
  378. if (skb->len >= hdrlen + mic_len &&
  379. !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  380. skb_trim(skb, skb->len - mic_len);
  381. if (skb->len >= hdrlen + iv_len) {
  382. memmove(skb->data + iv_len, skb->data, hdrlen);
  383. hdr = (struct ieee80211_hdr *)skb_pull(skb, iv_len);
  384. }
  385. no_key:
  386. if (ieee80211_is_data_qos(hdr->frame_control)) {
  387. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  388. memmove(skb->data + IEEE80211_QOS_CTL_LEN, skb->data,
  389. hdrlen - IEEE80211_QOS_CTL_LEN);
  390. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  391. }
  392. }
  393. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  394. struct sta_info *sta,
  395. struct sk_buff *skb)
  396. {
  397. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  398. sta->tx_filtered_count++;
  399. /*
  400. * Clear the TX filter mask for this STA when sending the next
  401. * packet. If the STA went to power save mode, this will happen
  402. * when it wakes up for the next time.
  403. */
  404. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  405. /*
  406. * This code races in the following way:
  407. *
  408. * (1) STA sends frame indicating it will go to sleep and does so
  409. * (2) hardware/firmware adds STA to filter list, passes frame up
  410. * (3) hardware/firmware processes TX fifo and suppresses a frame
  411. * (4) we get TX status before having processed the frame and
  412. * knowing that the STA has gone to sleep.
  413. *
  414. * This is actually quite unlikely even when both those events are
  415. * processed from interrupts coming in quickly after one another or
  416. * even at the same time because we queue both TX status events and
  417. * RX frames to be processed by a tasklet and process them in the
  418. * same order that they were received or TX status last. Hence, there
  419. * is no race as long as the frame RX is processed before the next TX
  420. * status, which drivers can ensure, see below.
  421. *
  422. * Note that this can only happen if the hardware or firmware can
  423. * actually add STAs to the filter list, if this is done by the
  424. * driver in response to set_tim() (which will only reduce the race
  425. * this whole filtering tries to solve, not completely solve it)
  426. * this situation cannot happen.
  427. *
  428. * To completely solve this race drivers need to make sure that they
  429. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  430. * functions and
  431. * (b) always process RX events before TX status events if ordering
  432. * can be unknown, for example with different interrupt status
  433. * bits.
  434. */
  435. if (test_sta_flags(sta, WLAN_STA_PS) &&
  436. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  437. ieee80211_remove_tx_extra(local, sta->key, skb);
  438. skb_queue_tail(&sta->tx_filtered, skb);
  439. return;
  440. }
  441. if (!test_sta_flags(sta, WLAN_STA_PS) &&
  442. !(info->flags & IEEE80211_TX_CTL_REQUEUE)) {
  443. /* Software retry the packet once */
  444. info->flags |= IEEE80211_TX_CTL_REQUEUE;
  445. ieee80211_remove_tx_extra(local, sta->key, skb);
  446. dev_queue_xmit(skb);
  447. return;
  448. }
  449. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  450. if (net_ratelimit())
  451. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  452. "queue_len=%d PS=%d @%lu\n",
  453. wiphy_name(local->hw.wiphy),
  454. skb_queue_len(&sta->tx_filtered),
  455. !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
  456. #endif
  457. dev_kfree_skb(skb);
  458. }
  459. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  460. {
  461. struct sk_buff *skb2;
  462. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  463. struct ieee80211_local *local = hw_to_local(hw);
  464. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  465. u16 frag, type;
  466. __le16 fc;
  467. struct ieee80211_supported_band *sband;
  468. struct ieee80211_tx_status_rtap_hdr *rthdr;
  469. struct ieee80211_sub_if_data *sdata;
  470. struct net_device *prev_dev = NULL;
  471. struct sta_info *sta;
  472. rcu_read_lock();
  473. sta = sta_info_get(local, hdr->addr1);
  474. if (sta) {
  475. if (info->status.excessive_retries &&
  476. test_sta_flags(sta, WLAN_STA_PS)) {
  477. /*
  478. * The STA is in power save mode, so assume
  479. * that this TX packet failed because of that.
  480. */
  481. ieee80211_handle_filtered_frame(local, sta, skb);
  482. rcu_read_unlock();
  483. return;
  484. }
  485. fc = hdr->frame_control;
  486. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  487. (ieee80211_is_data_qos(fc))) {
  488. u16 tid, ssn;
  489. u8 *qc;
  490. qc = ieee80211_get_qos_ctl(hdr);
  491. tid = qc[0] & 0xf;
  492. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  493. & IEEE80211_SCTL_SEQ);
  494. ieee80211_send_bar(sta->sdata, hdr->addr1,
  495. tid, ssn);
  496. }
  497. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  498. ieee80211_handle_filtered_frame(local, sta, skb);
  499. rcu_read_unlock();
  500. return;
  501. } else {
  502. if (info->status.excessive_retries)
  503. sta->tx_retry_failed++;
  504. sta->tx_retry_count += info->status.retry_count;
  505. }
  506. sband = local->hw.wiphy->bands[info->band];
  507. rate_control_tx_status(local, sband, sta, skb);
  508. }
  509. rcu_read_unlock();
  510. ieee80211_led_tx(local, 0);
  511. /* SNMP counters
  512. * Fragments are passed to low-level drivers as separate skbs, so these
  513. * are actually fragments, not frames. Update frame counters only for
  514. * the first fragment of the frame. */
  515. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  516. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  517. if (info->flags & IEEE80211_TX_STAT_ACK) {
  518. if (frag == 0) {
  519. local->dot11TransmittedFrameCount++;
  520. if (is_multicast_ether_addr(hdr->addr1))
  521. local->dot11MulticastTransmittedFrameCount++;
  522. if (info->status.retry_count > 0)
  523. local->dot11RetryCount++;
  524. if (info->status.retry_count > 1)
  525. local->dot11MultipleRetryCount++;
  526. }
  527. /* This counter shall be incremented for an acknowledged MPDU
  528. * with an individual address in the address 1 field or an MPDU
  529. * with a multicast address in the address 1 field of type Data
  530. * or Management. */
  531. if (!is_multicast_ether_addr(hdr->addr1) ||
  532. type == IEEE80211_FTYPE_DATA ||
  533. type == IEEE80211_FTYPE_MGMT)
  534. local->dot11TransmittedFragmentCount++;
  535. } else {
  536. if (frag == 0)
  537. local->dot11FailedCount++;
  538. }
  539. /* this was a transmitted frame, but now we want to reuse it */
  540. skb_orphan(skb);
  541. /*
  542. * This is a bit racy but we can avoid a lot of work
  543. * with this test...
  544. */
  545. if (!local->monitors && !local->cooked_mntrs) {
  546. dev_kfree_skb(skb);
  547. return;
  548. }
  549. /* send frame to monitor interfaces now */
  550. if (skb_headroom(skb) < sizeof(*rthdr)) {
  551. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  552. dev_kfree_skb(skb);
  553. return;
  554. }
  555. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  556. skb_push(skb, sizeof(*rthdr));
  557. memset(rthdr, 0, sizeof(*rthdr));
  558. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  559. rthdr->hdr.it_present =
  560. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  561. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  562. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  563. !is_multicast_ether_addr(hdr->addr1))
  564. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  565. if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) &&
  566. (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT))
  567. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  568. else if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  569. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  570. rthdr->data_retries = info->status.retry_count;
  571. /* XXX: is this sufficient for BPF? */
  572. skb_set_mac_header(skb, 0);
  573. skb->ip_summed = CHECKSUM_UNNECESSARY;
  574. skb->pkt_type = PACKET_OTHERHOST;
  575. skb->protocol = htons(ETH_P_802_2);
  576. memset(skb->cb, 0, sizeof(skb->cb));
  577. rcu_read_lock();
  578. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  579. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  580. if (!netif_running(sdata->dev))
  581. continue;
  582. if (prev_dev) {
  583. skb2 = skb_clone(skb, GFP_ATOMIC);
  584. if (skb2) {
  585. skb2->dev = prev_dev;
  586. netif_rx(skb2);
  587. }
  588. }
  589. prev_dev = sdata->dev;
  590. }
  591. }
  592. if (prev_dev) {
  593. skb->dev = prev_dev;
  594. netif_rx(skb);
  595. skb = NULL;
  596. }
  597. rcu_read_unlock();
  598. dev_kfree_skb(skb);
  599. }
  600. EXPORT_SYMBOL(ieee80211_tx_status);
  601. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  602. const struct ieee80211_ops *ops)
  603. {
  604. struct ieee80211_local *local;
  605. int priv_size;
  606. struct wiphy *wiphy;
  607. /* Ensure 32-byte alignment of our private data and hw private data.
  608. * We use the wiphy priv data for both our ieee80211_local and for
  609. * the driver's private data
  610. *
  611. * In memory it'll be like this:
  612. *
  613. * +-------------------------+
  614. * | struct wiphy |
  615. * +-------------------------+
  616. * | struct ieee80211_local |
  617. * +-------------------------+
  618. * | driver's private data |
  619. * +-------------------------+
  620. *
  621. */
  622. priv_size = ((sizeof(struct ieee80211_local) +
  623. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  624. priv_data_len;
  625. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  626. if (!wiphy)
  627. return NULL;
  628. wiphy->privid = mac80211_wiphy_privid;
  629. local = wiphy_priv(wiphy);
  630. local->hw.wiphy = wiphy;
  631. local->hw.priv = (char *)local +
  632. ((sizeof(struct ieee80211_local) +
  633. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  634. BUG_ON(!ops->tx);
  635. BUG_ON(!ops->start);
  636. BUG_ON(!ops->stop);
  637. BUG_ON(!ops->config);
  638. BUG_ON(!ops->add_interface);
  639. BUG_ON(!ops->remove_interface);
  640. BUG_ON(!ops->configure_filter);
  641. local->ops = ops;
  642. local->hw.queues = 1; /* default */
  643. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  644. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  645. local->short_retry_limit = 7;
  646. local->long_retry_limit = 4;
  647. local->hw.conf.radio_enabled = 1;
  648. INIT_LIST_HEAD(&local->interfaces);
  649. spin_lock_init(&local->key_lock);
  650. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  651. sta_info_init(local);
  652. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  653. (unsigned long)local);
  654. tasklet_disable(&local->tx_pending_tasklet);
  655. tasklet_init(&local->tasklet,
  656. ieee80211_tasklet_handler,
  657. (unsigned long) local);
  658. tasklet_disable(&local->tasklet);
  659. skb_queue_head_init(&local->skb_queue);
  660. skb_queue_head_init(&local->skb_queue_unreliable);
  661. return local_to_hw(local);
  662. }
  663. EXPORT_SYMBOL(ieee80211_alloc_hw);
  664. int ieee80211_register_hw(struct ieee80211_hw *hw)
  665. {
  666. struct ieee80211_local *local = hw_to_local(hw);
  667. const char *name;
  668. int result;
  669. enum ieee80211_band band;
  670. struct net_device *mdev;
  671. struct ieee80211_master_priv *mpriv;
  672. /*
  673. * generic code guarantees at least one band,
  674. * set this very early because much code assumes
  675. * that hw.conf.channel is assigned
  676. */
  677. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  678. struct ieee80211_supported_band *sband;
  679. sband = local->hw.wiphy->bands[band];
  680. if (sband) {
  681. /* init channel we're on */
  682. local->hw.conf.channel =
  683. local->oper_channel =
  684. local->scan_channel = &sband->channels[0];
  685. break;
  686. }
  687. }
  688. /* if low-level driver supports AP, we also support VLAN */
  689. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  690. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  691. /* mac80211 always supports monitor */
  692. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  693. result = wiphy_register(local->hw.wiphy);
  694. if (result < 0)
  695. return result;
  696. /*
  697. * We use the number of queues for feature tests (QoS, HT) internally
  698. * so restrict them appropriately.
  699. */
  700. if (hw->queues > IEEE80211_MAX_QUEUES)
  701. hw->queues = IEEE80211_MAX_QUEUES;
  702. if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
  703. hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
  704. if (hw->queues < 4)
  705. hw->ampdu_queues = 0;
  706. mdev = alloc_netdev_mq(sizeof(struct ieee80211_master_priv),
  707. "wmaster%d", ether_setup,
  708. ieee80211_num_queues(hw));
  709. if (!mdev)
  710. goto fail_mdev_alloc;
  711. mpriv = netdev_priv(mdev);
  712. mpriv->local = local;
  713. local->mdev = mdev;
  714. ieee80211_rx_bss_list_init(local);
  715. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  716. mdev->open = ieee80211_master_open;
  717. mdev->stop = ieee80211_master_stop;
  718. mdev->type = ARPHRD_IEEE80211;
  719. mdev->header_ops = &ieee80211_header_ops;
  720. mdev->set_multicast_list = ieee80211_master_set_multicast_list;
  721. name = wiphy_dev(local->hw.wiphy)->driver->name;
  722. local->hw.workqueue = create_freezeable_workqueue(name);
  723. if (!local->hw.workqueue) {
  724. result = -ENOMEM;
  725. goto fail_workqueue;
  726. }
  727. /*
  728. * The hardware needs headroom for sending the frame,
  729. * and we need some headroom for passing the frame to monitor
  730. * interfaces, but never both at the same time.
  731. */
  732. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  733. sizeof(struct ieee80211_tx_status_rtap_hdr));
  734. debugfs_hw_add(local);
  735. if (local->hw.conf.beacon_int < 10)
  736. local->hw.conf.beacon_int = 100;
  737. if (local->hw.max_listen_interval == 0)
  738. local->hw.max_listen_interval = 1;
  739. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  740. local->wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
  741. IEEE80211_HW_SIGNAL_DB |
  742. IEEE80211_HW_SIGNAL_DBM) ?
  743. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  744. local->wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
  745. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  746. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  747. local->wstats_flags |= IW_QUAL_DBM;
  748. result = sta_info_start(local);
  749. if (result < 0)
  750. goto fail_sta_info;
  751. rtnl_lock();
  752. result = dev_alloc_name(local->mdev, local->mdev->name);
  753. if (result < 0)
  754. goto fail_dev;
  755. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  756. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  757. result = register_netdevice(local->mdev);
  758. if (result < 0)
  759. goto fail_dev;
  760. result = ieee80211_init_rate_ctrl_alg(local,
  761. hw->rate_control_algorithm);
  762. if (result < 0) {
  763. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  764. "algorithm\n", wiphy_name(local->hw.wiphy));
  765. goto fail_rate;
  766. }
  767. result = ieee80211_wep_init(local);
  768. if (result < 0) {
  769. printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
  770. wiphy_name(local->hw.wiphy), result);
  771. goto fail_wep;
  772. }
  773. local->mdev->select_queue = ieee80211_select_queue;
  774. /* add one default STA interface */
  775. result = ieee80211_if_add(local, "wlan%d", NULL,
  776. NL80211_IFTYPE_STATION, NULL);
  777. if (result)
  778. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  779. wiphy_name(local->hw.wiphy));
  780. rtnl_unlock();
  781. ieee80211_led_init(local);
  782. return 0;
  783. fail_wep:
  784. rate_control_deinitialize(local);
  785. fail_rate:
  786. unregister_netdevice(local->mdev);
  787. local->mdev = NULL;
  788. fail_dev:
  789. rtnl_unlock();
  790. sta_info_stop(local);
  791. fail_sta_info:
  792. debugfs_hw_del(local);
  793. destroy_workqueue(local->hw.workqueue);
  794. fail_workqueue:
  795. if (local->mdev)
  796. free_netdev(local->mdev);
  797. fail_mdev_alloc:
  798. wiphy_unregister(local->hw.wiphy);
  799. return result;
  800. }
  801. EXPORT_SYMBOL(ieee80211_register_hw);
  802. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  803. {
  804. struct ieee80211_local *local = hw_to_local(hw);
  805. tasklet_kill(&local->tx_pending_tasklet);
  806. tasklet_kill(&local->tasklet);
  807. rtnl_lock();
  808. /*
  809. * At this point, interface list manipulations are fine
  810. * because the driver cannot be handing us frames any
  811. * more and the tasklet is killed.
  812. */
  813. /* First, we remove all virtual interfaces. */
  814. ieee80211_remove_interfaces(local);
  815. /* then, finally, remove the master interface */
  816. unregister_netdevice(local->mdev);
  817. rtnl_unlock();
  818. ieee80211_rx_bss_list_deinit(local);
  819. ieee80211_clear_tx_pending(local);
  820. sta_info_stop(local);
  821. rate_control_deinitialize(local);
  822. debugfs_hw_del(local);
  823. if (skb_queue_len(&local->skb_queue)
  824. || skb_queue_len(&local->skb_queue_unreliable))
  825. printk(KERN_WARNING "%s: skb_queue not empty\n",
  826. wiphy_name(local->hw.wiphy));
  827. skb_queue_purge(&local->skb_queue);
  828. skb_queue_purge(&local->skb_queue_unreliable);
  829. destroy_workqueue(local->hw.workqueue);
  830. wiphy_unregister(local->hw.wiphy);
  831. ieee80211_wep_free(local);
  832. ieee80211_led_exit(local);
  833. free_netdev(local->mdev);
  834. }
  835. EXPORT_SYMBOL(ieee80211_unregister_hw);
  836. void ieee80211_free_hw(struct ieee80211_hw *hw)
  837. {
  838. struct ieee80211_local *local = hw_to_local(hw);
  839. wiphy_free(local->hw.wiphy);
  840. }
  841. EXPORT_SYMBOL(ieee80211_free_hw);
  842. static int __init ieee80211_init(void)
  843. {
  844. struct sk_buff *skb;
  845. int ret;
  846. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  847. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  848. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  849. ret = rc80211_minstrel_init();
  850. if (ret)
  851. return ret;
  852. ret = rc80211_pid_init();
  853. if (ret)
  854. return ret;
  855. ieee80211_debugfs_netdev_init();
  856. return 0;
  857. }
  858. static void __exit ieee80211_exit(void)
  859. {
  860. rc80211_pid_exit();
  861. rc80211_minstrel_exit();
  862. /*
  863. * For key todo, it'll be empty by now but the work
  864. * might still be scheduled.
  865. */
  866. flush_scheduled_work();
  867. if (mesh_allocated)
  868. ieee80211s_stop();
  869. ieee80211_debugfs_netdev_exit();
  870. }
  871. subsys_initcall(ieee80211_init);
  872. module_exit(ieee80211_exit);
  873. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  874. MODULE_LICENSE("GPL");