main.c 29 KB

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