main.c 31 KB

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