main.c 27 KB

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