main.c 28 KB

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