main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  131. return -EINVAL;
  132. if (!local->ops->config_interface)
  133. return 0;
  134. memset(&conf, 0, sizeof(conf));
  135. if (sdata->vif.type == NL80211_IFTYPE_STATION ||
  136. sdata->vif.type == NL80211_IFTYPE_ADHOC)
  137. conf.bssid = sdata->u.sta.bssid;
  138. else if (sdata->vif.type == NL80211_IFTYPE_AP)
  139. conf.bssid = sdata->dev->dev_addr;
  140. else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  141. static const u8 zero[ETH_ALEN] = { 0 };
  142. conf.bssid = zero;
  143. } else {
  144. WARN_ON(1);
  145. return -EINVAL;
  146. }
  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. case NL80211_IFTYPE_MESH_POINT:
  175. conf.enable_beacon = true;
  176. break;
  177. default:
  178. /* not reached */
  179. WARN_ON(1);
  180. break;
  181. }
  182. }
  183. }
  184. if (WARN_ON(!conf.bssid && (changed & IEEE80211_IFCC_BSSID)))
  185. return -EINVAL;
  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)
  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. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  600. const struct ieee80211_ops *ops)
  601. {
  602. struct ieee80211_local *local;
  603. int priv_size;
  604. struct wiphy *wiphy;
  605. /* Ensure 32-byte alignment of our private data and hw private data.
  606. * We use the wiphy priv data for both our ieee80211_local and for
  607. * the driver's private data
  608. *
  609. * In memory it'll be like this:
  610. *
  611. * +-------------------------+
  612. * | struct wiphy |
  613. * +-------------------------+
  614. * | struct ieee80211_local |
  615. * +-------------------------+
  616. * | driver's private data |
  617. * +-------------------------+
  618. *
  619. */
  620. priv_size = ((sizeof(struct ieee80211_local) +
  621. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  622. priv_data_len;
  623. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  624. if (!wiphy)
  625. return NULL;
  626. wiphy->privid = mac80211_wiphy_privid;
  627. local = wiphy_priv(wiphy);
  628. local->hw.wiphy = wiphy;
  629. local->hw.priv = (char *)local +
  630. ((sizeof(struct ieee80211_local) +
  631. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  632. BUG_ON(!ops->tx);
  633. BUG_ON(!ops->start);
  634. BUG_ON(!ops->stop);
  635. BUG_ON(!ops->config);
  636. BUG_ON(!ops->add_interface);
  637. BUG_ON(!ops->remove_interface);
  638. BUG_ON(!ops->configure_filter);
  639. local->ops = ops;
  640. /* set up some defaults */
  641. local->hw.queues = 1;
  642. local->hw.max_rates = 1;
  643. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  644. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  645. local->hw.conf.long_frame_max_tx_count = 4;
  646. local->hw.conf.short_frame_max_tx_count = 7;
  647. local->hw.conf.radio_enabled = true;
  648. INIT_LIST_HEAD(&local->interfaces);
  649. mutex_init(&local->iflist_mtx);
  650. spin_lock_init(&local->key_lock);
  651. spin_lock_init(&local->queue_stop_reason_lock);
  652. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  653. INIT_WORK(&local->dynamic_ps_enable_work,
  654. ieee80211_dynamic_ps_enable_work);
  655. INIT_WORK(&local->dynamic_ps_disable_work,
  656. ieee80211_dynamic_ps_disable_work);
  657. setup_timer(&local->dynamic_ps_timer,
  658. ieee80211_dynamic_ps_timer, (unsigned long) local);
  659. sta_info_init(local);
  660. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  661. (unsigned long)local);
  662. tasklet_disable(&local->tx_pending_tasklet);
  663. tasklet_init(&local->tasklet,
  664. ieee80211_tasklet_handler,
  665. (unsigned long) local);
  666. tasklet_disable(&local->tasklet);
  667. skb_queue_head_init(&local->skb_queue);
  668. skb_queue_head_init(&local->skb_queue_unreliable);
  669. return local_to_hw(local);
  670. }
  671. EXPORT_SYMBOL(ieee80211_alloc_hw);
  672. int ieee80211_register_hw(struct ieee80211_hw *hw)
  673. {
  674. struct ieee80211_local *local = hw_to_local(hw);
  675. int result;
  676. enum ieee80211_band band;
  677. struct net_device *mdev;
  678. struct ieee80211_master_priv *mpriv;
  679. /*
  680. * generic code guarantees at least one band,
  681. * set this very early because much code assumes
  682. * that hw.conf.channel is assigned
  683. */
  684. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  685. struct ieee80211_supported_band *sband;
  686. sband = local->hw.wiphy->bands[band];
  687. if (sband) {
  688. /* init channel we're on */
  689. local->hw.conf.channel =
  690. local->oper_channel =
  691. local->scan_channel = &sband->channels[0];
  692. break;
  693. }
  694. }
  695. /* if low-level driver supports AP, we also support VLAN */
  696. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  697. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  698. /* mac80211 always supports monitor */
  699. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  700. result = wiphy_register(local->hw.wiphy);
  701. if (result < 0)
  702. return result;
  703. /*
  704. * We use the number of queues for feature tests (QoS, HT) internally
  705. * so restrict them appropriately.
  706. */
  707. if (hw->queues > IEEE80211_MAX_QUEUES)
  708. hw->queues = IEEE80211_MAX_QUEUES;
  709. if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
  710. hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
  711. if (hw->queues < 4)
  712. hw->ampdu_queues = 0;
  713. mdev = alloc_netdev_mq(sizeof(struct ieee80211_master_priv),
  714. "wmaster%d", ether_setup,
  715. ieee80211_num_queues(hw));
  716. if (!mdev)
  717. goto fail_mdev_alloc;
  718. mpriv = netdev_priv(mdev);
  719. mpriv->local = local;
  720. local->mdev = mdev;
  721. ieee80211_rx_bss_list_init(local);
  722. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  723. mdev->open = ieee80211_master_open;
  724. mdev->stop = ieee80211_master_stop;
  725. mdev->type = ARPHRD_IEEE80211;
  726. mdev->header_ops = &ieee80211_header_ops;
  727. mdev->set_multicast_list = ieee80211_master_set_multicast_list;
  728. local->hw.workqueue =
  729. create_singlethread_workqueue(wiphy_name(local->hw.wiphy));
  730. if (!local->hw.workqueue) {
  731. result = -ENOMEM;
  732. goto fail_workqueue;
  733. }
  734. /*
  735. * The hardware needs headroom for sending the frame,
  736. * and we need some headroom for passing the frame to monitor
  737. * interfaces, but never both at the same time.
  738. */
  739. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  740. sizeof(struct ieee80211_tx_status_rtap_hdr));
  741. debugfs_hw_add(local);
  742. if (local->hw.conf.beacon_int < 10)
  743. local->hw.conf.beacon_int = 100;
  744. if (local->hw.max_listen_interval == 0)
  745. local->hw.max_listen_interval = 1;
  746. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  747. local->wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
  748. IEEE80211_HW_SIGNAL_DB |
  749. IEEE80211_HW_SIGNAL_DBM) ?
  750. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  751. local->wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
  752. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  753. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  754. local->wstats_flags |= IW_QUAL_DBM;
  755. result = sta_info_start(local);
  756. if (result < 0)
  757. goto fail_sta_info;
  758. rtnl_lock();
  759. result = dev_alloc_name(local->mdev, local->mdev->name);
  760. if (result < 0)
  761. goto fail_dev;
  762. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  763. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  764. result = register_netdevice(local->mdev);
  765. if (result < 0)
  766. goto fail_dev;
  767. result = ieee80211_init_rate_ctrl_alg(local,
  768. hw->rate_control_algorithm);
  769. if (result < 0) {
  770. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  771. "algorithm\n", wiphy_name(local->hw.wiphy));
  772. goto fail_rate;
  773. }
  774. result = ieee80211_wep_init(local);
  775. if (result < 0) {
  776. printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
  777. wiphy_name(local->hw.wiphy), result);
  778. goto fail_wep;
  779. }
  780. local->mdev->select_queue = ieee80211_select_queue;
  781. /* add one default STA interface if supported */
  782. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
  783. result = ieee80211_if_add(local, "wlan%d", NULL,
  784. NL80211_IFTYPE_STATION, NULL);
  785. if (result)
  786. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  787. wiphy_name(local->hw.wiphy));
  788. }
  789. rtnl_unlock();
  790. ieee80211_led_init(local);
  791. return 0;
  792. fail_wep:
  793. rate_control_deinitialize(local);
  794. fail_rate:
  795. unregister_netdevice(local->mdev);
  796. local->mdev = NULL;
  797. fail_dev:
  798. rtnl_unlock();
  799. sta_info_stop(local);
  800. fail_sta_info:
  801. debugfs_hw_del(local);
  802. destroy_workqueue(local->hw.workqueue);
  803. fail_workqueue:
  804. if (local->mdev)
  805. free_netdev(local->mdev);
  806. fail_mdev_alloc:
  807. wiphy_unregister(local->hw.wiphy);
  808. return result;
  809. }
  810. EXPORT_SYMBOL(ieee80211_register_hw);
  811. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  812. {
  813. struct ieee80211_local *local = hw_to_local(hw);
  814. tasklet_kill(&local->tx_pending_tasklet);
  815. tasklet_kill(&local->tasklet);
  816. rtnl_lock();
  817. /*
  818. * At this point, interface list manipulations are fine
  819. * because the driver cannot be handing us frames any
  820. * more and the tasklet is killed.
  821. */
  822. /* First, we remove all virtual interfaces. */
  823. ieee80211_remove_interfaces(local);
  824. /* then, finally, remove the master interface */
  825. unregister_netdevice(local->mdev);
  826. rtnl_unlock();
  827. ieee80211_rx_bss_list_deinit(local);
  828. ieee80211_clear_tx_pending(local);
  829. sta_info_stop(local);
  830. rate_control_deinitialize(local);
  831. debugfs_hw_del(local);
  832. if (skb_queue_len(&local->skb_queue)
  833. || skb_queue_len(&local->skb_queue_unreliable))
  834. printk(KERN_WARNING "%s: skb_queue not empty\n",
  835. wiphy_name(local->hw.wiphy));
  836. skb_queue_purge(&local->skb_queue);
  837. skb_queue_purge(&local->skb_queue_unreliable);
  838. destroy_workqueue(local->hw.workqueue);
  839. wiphy_unregister(local->hw.wiphy);
  840. ieee80211_wep_free(local);
  841. ieee80211_led_exit(local);
  842. free_netdev(local->mdev);
  843. }
  844. EXPORT_SYMBOL(ieee80211_unregister_hw);
  845. void ieee80211_free_hw(struct ieee80211_hw *hw)
  846. {
  847. struct ieee80211_local *local = hw_to_local(hw);
  848. mutex_destroy(&local->iflist_mtx);
  849. wiphy_free(local->hw.wiphy);
  850. }
  851. EXPORT_SYMBOL(ieee80211_free_hw);
  852. static int __init ieee80211_init(void)
  853. {
  854. struct sk_buff *skb;
  855. int ret;
  856. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  857. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  858. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  859. ret = rc80211_minstrel_init();
  860. if (ret)
  861. return ret;
  862. ret = rc80211_pid_init();
  863. if (ret)
  864. return ret;
  865. ieee80211_debugfs_netdev_init();
  866. return 0;
  867. }
  868. static void __exit ieee80211_exit(void)
  869. {
  870. rc80211_pid_exit();
  871. rc80211_minstrel_exit();
  872. /*
  873. * For key todo, it'll be empty by now but the work
  874. * might still be scheduled.
  875. */
  876. flush_scheduled_work();
  877. if (mesh_allocated)
  878. ieee80211s_stop();
  879. ieee80211_debugfs_netdev_exit();
  880. }
  881. subsys_initcall(ieee80211_init);
  882. module_exit(ieee80211_exit);
  883. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  884. MODULE_LICENSE("GPL");