main.c 27 KB

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