main.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/bitmap.h>
  23. #include <linux/pm_qos_params.h>
  24. #include <net/net_namespace.h>
  25. #include <net/cfg80211.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wep.h"
  31. #include "wme.h"
  32. #include "aes_ccm.h"
  33. #include "led.h"
  34. #include "cfg.h"
  35. #include "debugfs.h"
  36. #include "debugfs_netdev.h"
  37. /*
  38. * For seeing transmitted packets on monitor interfaces
  39. * we have a radiotap header too.
  40. */
  41. struct ieee80211_tx_status_rtap_hdr {
  42. struct ieee80211_radiotap_header hdr;
  43. u8 rate;
  44. u8 padding_for_rate;
  45. __le16 tx_flags;
  46. u8 data_retries;
  47. } __attribute__ ((packed));
  48. /* must be called under mdev tx lock */
  49. void ieee80211_configure_filter(struct ieee80211_local *local)
  50. {
  51. unsigned int changed_flags;
  52. unsigned int new_flags = 0;
  53. if (atomic_read(&local->iff_promiscs))
  54. new_flags |= FIF_PROMISC_IN_BSS;
  55. if (atomic_read(&local->iff_allmultis))
  56. new_flags |= FIF_ALLMULTI;
  57. if (local->monitors)
  58. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  59. if (local->fif_fcsfail)
  60. new_flags |= FIF_FCSFAIL;
  61. if (local->fif_plcpfail)
  62. new_flags |= FIF_PLCPFAIL;
  63. if (local->fif_control)
  64. new_flags |= FIF_CONTROL;
  65. if (local->fif_other_bss)
  66. new_flags |= FIF_OTHER_BSS;
  67. changed_flags = local->filter_flags ^ new_flags;
  68. /* be a bit nasty */
  69. new_flags |= (1<<31);
  70. drv_configure_filter(local, changed_flags, &new_flags,
  71. local->mc_count,
  72. local->mc_list);
  73. WARN_ON(new_flags & (1<<31));
  74. local->filter_flags = new_flags & ~(1<<31);
  75. }
  76. int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
  77. {
  78. struct ieee80211_channel *chan, *scan_chan;
  79. int ret = 0;
  80. int power;
  81. enum nl80211_channel_type channel_type;
  82. might_sleep();
  83. scan_chan = local->scan_channel;
  84. if (scan_chan) {
  85. chan = scan_chan;
  86. channel_type = NL80211_CHAN_NO_HT;
  87. } else {
  88. chan = local->oper_channel;
  89. channel_type = local->oper_channel_type;
  90. }
  91. if (chan != local->hw.conf.channel ||
  92. channel_type != local->hw.conf.channel_type) {
  93. local->hw.conf.channel = chan;
  94. local->hw.conf.channel_type = channel_type;
  95. changed |= IEEE80211_CONF_CHANGE_CHANNEL;
  96. }
  97. if (scan_chan)
  98. power = chan->max_power;
  99. else
  100. power = local->power_constr_level ?
  101. (chan->max_power - local->power_constr_level) :
  102. chan->max_power;
  103. if (local->user_power_level >= 0)
  104. power = min(power, local->user_power_level);
  105. if (local->hw.conf.power_level != power) {
  106. changed |= IEEE80211_CONF_CHANGE_POWER;
  107. local->hw.conf.power_level = power;
  108. }
  109. if (changed && local->open_count) {
  110. ret = drv_config(local, changed);
  111. /*
  112. * Goal:
  113. * HW reconfiguration should never fail, the driver has told
  114. * us what it can support so it should live up to that promise.
  115. *
  116. * Current status:
  117. * rfkill is not integrated with mac80211 and a
  118. * configuration command can thus fail if hardware rfkill
  119. * is enabled
  120. *
  121. * FIXME: integrate rfkill with mac80211 and then add this
  122. * WARN_ON() back
  123. *
  124. */
  125. /* WARN_ON(ret); */
  126. }
  127. return ret;
  128. }
  129. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  130. u32 changed)
  131. {
  132. struct ieee80211_local *local = sdata->local;
  133. static const u8 zero[ETH_ALEN] = { 0 };
  134. if (!changed)
  135. return;
  136. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  137. /*
  138. * While not associated, claim a BSSID of all-zeroes
  139. * so that drivers don't do any weird things with the
  140. * BSSID at that time.
  141. */
  142. if (sdata->vif.bss_conf.assoc)
  143. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  144. else
  145. sdata->vif.bss_conf.bssid = zero;
  146. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  147. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  148. else if (sdata->vif.type == NL80211_IFTYPE_AP)
  149. sdata->vif.bss_conf.bssid = sdata->dev->dev_addr;
  150. else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  151. sdata->vif.bss_conf.bssid = zero;
  152. } else {
  153. WARN_ON(1);
  154. return;
  155. }
  156. switch (sdata->vif.type) {
  157. case NL80211_IFTYPE_AP:
  158. case NL80211_IFTYPE_ADHOC:
  159. case NL80211_IFTYPE_MESH_POINT:
  160. break;
  161. default:
  162. /* do not warn to simplify caller in scan.c */
  163. changed &= ~BSS_CHANGED_BEACON_ENABLED;
  164. if (WARN_ON(changed & BSS_CHANGED_BEACON))
  165. return;
  166. break;
  167. }
  168. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  169. if (local->quiescing || !netif_running(sdata->dev) ||
  170. test_bit(SCAN_SW_SCANNING, &local->scanning)) {
  171. sdata->vif.bss_conf.enable_beacon = false;
  172. } else {
  173. /*
  174. * Beacon should be enabled, but AP mode must
  175. * check whether there is a beacon configured.
  176. */
  177. switch (sdata->vif.type) {
  178. case NL80211_IFTYPE_AP:
  179. sdata->vif.bss_conf.enable_beacon =
  180. !!rcu_dereference(sdata->u.ap.beacon);
  181. break;
  182. case NL80211_IFTYPE_ADHOC:
  183. sdata->vif.bss_conf.enable_beacon =
  184. !!rcu_dereference(sdata->u.ibss.presp);
  185. break;
  186. case NL80211_IFTYPE_MESH_POINT:
  187. sdata->vif.bss_conf.enable_beacon = true;
  188. break;
  189. default:
  190. /* not reached */
  191. WARN_ON(1);
  192. break;
  193. }
  194. }
  195. }
  196. drv_bss_info_changed(local, &sdata->vif,
  197. &sdata->vif.bss_conf, changed);
  198. /* DEPRECATED */
  199. local->hw.conf.beacon_int = sdata->vif.bss_conf.beacon_int;
  200. }
  201. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  202. {
  203. sdata->vif.bss_conf.use_cts_prot = false;
  204. sdata->vif.bss_conf.use_short_preamble = false;
  205. sdata->vif.bss_conf.use_short_slot = false;
  206. return BSS_CHANGED_ERP_CTS_PROT |
  207. BSS_CHANGED_ERP_PREAMBLE |
  208. BSS_CHANGED_ERP_SLOT;
  209. }
  210. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  211. struct sk_buff *skb)
  212. {
  213. struct ieee80211_local *local = hw_to_local(hw);
  214. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  215. int tmp;
  216. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  217. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  218. &local->skb_queue : &local->skb_queue_unreliable, skb);
  219. tmp = skb_queue_len(&local->skb_queue) +
  220. skb_queue_len(&local->skb_queue_unreliable);
  221. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  222. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  223. dev_kfree_skb_irq(skb);
  224. tmp--;
  225. I802_DEBUG_INC(local->tx_status_drop);
  226. }
  227. tasklet_schedule(&local->tasklet);
  228. }
  229. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  230. static void ieee80211_tasklet_handler(unsigned long data)
  231. {
  232. struct ieee80211_local *local = (struct ieee80211_local *) data;
  233. struct sk_buff *skb;
  234. struct ieee80211_ra_tid *ra_tid;
  235. while ((skb = skb_dequeue(&local->skb_queue)) ||
  236. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  237. switch (skb->pkt_type) {
  238. case IEEE80211_RX_MSG:
  239. /* Clear skb->pkt_type in order to not confuse kernel
  240. * netstack. */
  241. skb->pkt_type = 0;
  242. ieee80211_rx(local_to_hw(local), skb);
  243. break;
  244. case IEEE80211_TX_STATUS_MSG:
  245. skb->pkt_type = 0;
  246. ieee80211_tx_status(local_to_hw(local), skb);
  247. break;
  248. case IEEE80211_DELBA_MSG:
  249. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  250. ieee80211_stop_tx_ba_cb(local_to_hw(local),
  251. ra_tid->ra, ra_tid->tid);
  252. dev_kfree_skb(skb);
  253. break;
  254. case IEEE80211_ADDBA_MSG:
  255. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  256. ieee80211_start_tx_ba_cb(local_to_hw(local),
  257. ra_tid->ra, ra_tid->tid);
  258. dev_kfree_skb(skb);
  259. break ;
  260. default:
  261. WARN(1, "mac80211: Packet is of unknown type %d\n",
  262. skb->pkt_type);
  263. dev_kfree_skb(skb);
  264. break;
  265. }
  266. }
  267. }
  268. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  269. struct sta_info *sta,
  270. struct sk_buff *skb)
  271. {
  272. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  273. /*
  274. * XXX: This is temporary!
  275. *
  276. * The problem here is that when we get here, the driver will
  277. * quite likely have pretty much overwritten info->control by
  278. * using info->driver_data or info->rate_driver_data. Thus,
  279. * when passing out the frame to the driver again, we would be
  280. * passing completely bogus data since the driver would then
  281. * expect a properly filled info->control. In mac80211 itself
  282. * the same problem occurs, since we need info->control.vif
  283. * internally.
  284. *
  285. * To fix this, we should send the frame through TX processing
  286. * again. However, it's not that simple, since the frame will
  287. * have been software-encrypted (if applicable) already, and
  288. * encrypting it again doesn't do much good. So to properly do
  289. * that, we not only have to skip the actual 'raw' encryption
  290. * (key selection etc. still has to be done!) but also the
  291. * sequence number assignment since that impacts the crypto
  292. * encapsulation, of course.
  293. *
  294. * Hence, for now, fix the bug by just dropping the frame.
  295. */
  296. goto drop;
  297. sta->tx_filtered_count++;
  298. /*
  299. * Clear the TX filter mask for this STA when sending the next
  300. * packet. If the STA went to power save mode, this will happen
  301. * when it wakes up for the next time.
  302. */
  303. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  304. /*
  305. * This code races in the following way:
  306. *
  307. * (1) STA sends frame indicating it will go to sleep and does so
  308. * (2) hardware/firmware adds STA to filter list, passes frame up
  309. * (3) hardware/firmware processes TX fifo and suppresses a frame
  310. * (4) we get TX status before having processed the frame and
  311. * knowing that the STA has gone to sleep.
  312. *
  313. * This is actually quite unlikely even when both those events are
  314. * processed from interrupts coming in quickly after one another or
  315. * even at the same time because we queue both TX status events and
  316. * RX frames to be processed by a tasklet and process them in the
  317. * same order that they were received or TX status last. Hence, there
  318. * is no race as long as the frame RX is processed before the next TX
  319. * status, which drivers can ensure, see below.
  320. *
  321. * Note that this can only happen if the hardware or firmware can
  322. * actually add STAs to the filter list, if this is done by the
  323. * driver in response to set_tim() (which will only reduce the race
  324. * this whole filtering tries to solve, not completely solve it)
  325. * this situation cannot happen.
  326. *
  327. * To completely solve this race drivers need to make sure that they
  328. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  329. * functions and
  330. * (b) always process RX events before TX status events if ordering
  331. * can be unknown, for example with different interrupt status
  332. * bits.
  333. */
  334. if (test_sta_flags(sta, WLAN_STA_PS) &&
  335. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  336. skb_queue_tail(&sta->tx_filtered, skb);
  337. return;
  338. }
  339. if (!test_sta_flags(sta, WLAN_STA_PS) &&
  340. !(info->flags & IEEE80211_TX_INTFL_RETRIED)) {
  341. /* Software retry the packet once */
  342. info->flags |= IEEE80211_TX_INTFL_RETRIED;
  343. ieee80211_add_pending_skb(local, skb);
  344. return;
  345. }
  346. drop:
  347. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  348. if (net_ratelimit())
  349. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  350. "queue_len=%d PS=%d @%lu\n",
  351. wiphy_name(local->hw.wiphy),
  352. skb_queue_len(&sta->tx_filtered),
  353. !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
  354. #endif
  355. dev_kfree_skb(skb);
  356. }
  357. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  358. {
  359. struct sk_buff *skb2;
  360. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  361. struct ieee80211_local *local = hw_to_local(hw);
  362. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  363. u16 frag, type;
  364. __le16 fc;
  365. struct ieee80211_supported_band *sband;
  366. struct ieee80211_tx_status_rtap_hdr *rthdr;
  367. struct ieee80211_sub_if_data *sdata;
  368. struct net_device *prev_dev = NULL;
  369. struct sta_info *sta;
  370. int retry_count = -1, i;
  371. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  372. /* the HW cannot have attempted that rate */
  373. if (i >= hw->max_rates) {
  374. info->status.rates[i].idx = -1;
  375. info->status.rates[i].count = 0;
  376. }
  377. retry_count += info->status.rates[i].count;
  378. }
  379. if (retry_count < 0)
  380. retry_count = 0;
  381. rcu_read_lock();
  382. sband = local->hw.wiphy->bands[info->band];
  383. sta = sta_info_get(local, hdr->addr1);
  384. if (sta) {
  385. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  386. test_sta_flags(sta, WLAN_STA_PS)) {
  387. /*
  388. * The STA is in power save mode, so assume
  389. * that this TX packet failed because of that.
  390. */
  391. ieee80211_handle_filtered_frame(local, sta, skb);
  392. rcu_read_unlock();
  393. return;
  394. }
  395. fc = hdr->frame_control;
  396. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  397. (ieee80211_is_data_qos(fc))) {
  398. u16 tid, ssn;
  399. u8 *qc;
  400. qc = ieee80211_get_qos_ctl(hdr);
  401. tid = qc[0] & 0xf;
  402. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  403. & IEEE80211_SCTL_SEQ);
  404. ieee80211_send_bar(sta->sdata, hdr->addr1,
  405. tid, ssn);
  406. }
  407. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  408. ieee80211_handle_filtered_frame(local, sta, skb);
  409. rcu_read_unlock();
  410. return;
  411. } else {
  412. if (!(info->flags & IEEE80211_TX_STAT_ACK))
  413. sta->tx_retry_failed++;
  414. sta->tx_retry_count += retry_count;
  415. }
  416. rate_control_tx_status(local, sband, sta, skb);
  417. }
  418. rcu_read_unlock();
  419. ieee80211_led_tx(local, 0);
  420. /* SNMP counters
  421. * Fragments are passed to low-level drivers as separate skbs, so these
  422. * are actually fragments, not frames. Update frame counters only for
  423. * the first fragment of the frame. */
  424. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  425. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  426. if (info->flags & IEEE80211_TX_STAT_ACK) {
  427. if (frag == 0) {
  428. local->dot11TransmittedFrameCount++;
  429. if (is_multicast_ether_addr(hdr->addr1))
  430. local->dot11MulticastTransmittedFrameCount++;
  431. if (retry_count > 0)
  432. local->dot11RetryCount++;
  433. if (retry_count > 1)
  434. local->dot11MultipleRetryCount++;
  435. }
  436. /* This counter shall be incremented for an acknowledged MPDU
  437. * with an individual address in the address 1 field or an MPDU
  438. * with a multicast address in the address 1 field of type Data
  439. * or Management. */
  440. if (!is_multicast_ether_addr(hdr->addr1) ||
  441. type == IEEE80211_FTYPE_DATA ||
  442. type == IEEE80211_FTYPE_MGMT)
  443. local->dot11TransmittedFragmentCount++;
  444. } else {
  445. if (frag == 0)
  446. local->dot11FailedCount++;
  447. }
  448. /* this was a transmitted frame, but now we want to reuse it */
  449. skb_orphan(skb);
  450. /*
  451. * This is a bit racy but we can avoid a lot of work
  452. * with this test...
  453. */
  454. if (!local->monitors && !local->cooked_mntrs) {
  455. dev_kfree_skb(skb);
  456. return;
  457. }
  458. /* send frame to monitor interfaces now */
  459. if (skb_headroom(skb) < sizeof(*rthdr)) {
  460. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  461. dev_kfree_skb(skb);
  462. return;
  463. }
  464. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  465. skb_push(skb, sizeof(*rthdr));
  466. memset(rthdr, 0, sizeof(*rthdr));
  467. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  468. rthdr->hdr.it_present =
  469. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  470. (1 << IEEE80211_RADIOTAP_DATA_RETRIES) |
  471. (1 << IEEE80211_RADIOTAP_RATE));
  472. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  473. !is_multicast_ether_addr(hdr->addr1))
  474. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  475. /*
  476. * XXX: Once radiotap gets the bitmap reset thing the vendor
  477. * extensions proposal contains, we can actually report
  478. * the whole set of tries we did.
  479. */
  480. if ((info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  481. (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  482. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  483. else if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  484. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  485. if (info->status.rates[0].idx >= 0 &&
  486. !(info->status.rates[0].flags & IEEE80211_TX_RC_MCS))
  487. rthdr->rate = sband->bitrates[
  488. info->status.rates[0].idx].bitrate / 5;
  489. /* for now report the total retry_count */
  490. rthdr->data_retries = retry_count;
  491. /* XXX: is this sufficient for BPF? */
  492. skb_set_mac_header(skb, 0);
  493. skb->ip_summed = CHECKSUM_UNNECESSARY;
  494. skb->pkt_type = PACKET_OTHERHOST;
  495. skb->protocol = htons(ETH_P_802_2);
  496. memset(skb->cb, 0, sizeof(skb->cb));
  497. rcu_read_lock();
  498. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  499. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  500. if (!netif_running(sdata->dev))
  501. continue;
  502. if (prev_dev) {
  503. skb2 = skb_clone(skb, GFP_ATOMIC);
  504. if (skb2) {
  505. skb2->dev = prev_dev;
  506. netif_rx(skb2);
  507. }
  508. }
  509. prev_dev = sdata->dev;
  510. }
  511. }
  512. if (prev_dev) {
  513. skb->dev = prev_dev;
  514. netif_rx(skb);
  515. skb = NULL;
  516. }
  517. rcu_read_unlock();
  518. dev_kfree_skb(skb);
  519. }
  520. EXPORT_SYMBOL(ieee80211_tx_status);
  521. static void ieee80211_restart_work(struct work_struct *work)
  522. {
  523. struct ieee80211_local *local =
  524. container_of(work, struct ieee80211_local, restart_work);
  525. rtnl_lock();
  526. ieee80211_reconfig(local);
  527. rtnl_unlock();
  528. }
  529. void ieee80211_restart_hw(struct ieee80211_hw *hw)
  530. {
  531. struct ieee80211_local *local = hw_to_local(hw);
  532. /* use this reason, __ieee80211_resume will unblock it */
  533. ieee80211_stop_queues_by_reason(hw,
  534. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  535. schedule_work(&local->restart_work);
  536. }
  537. EXPORT_SYMBOL(ieee80211_restart_hw);
  538. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  539. const struct ieee80211_ops *ops)
  540. {
  541. struct ieee80211_local *local;
  542. int priv_size, i;
  543. struct wiphy *wiphy;
  544. /* Ensure 32-byte alignment of our private data and hw private data.
  545. * We use the wiphy priv data for both our ieee80211_local and for
  546. * the driver's private data
  547. *
  548. * In memory it'll be like this:
  549. *
  550. * +-------------------------+
  551. * | struct wiphy |
  552. * +-------------------------+
  553. * | struct ieee80211_local |
  554. * +-------------------------+
  555. * | driver's private data |
  556. * +-------------------------+
  557. *
  558. */
  559. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  560. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  561. if (!wiphy)
  562. return NULL;
  563. wiphy->netnsok = true;
  564. wiphy->privid = mac80211_wiphy_privid;
  565. /* Yes, putting cfg80211_bss into ieee80211_bss is a hack */
  566. wiphy->bss_priv_size = sizeof(struct ieee80211_bss) -
  567. sizeof(struct cfg80211_bss);
  568. local = wiphy_priv(wiphy);
  569. local->hw.wiphy = wiphy;
  570. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  571. BUG_ON(!ops->tx);
  572. BUG_ON(!ops->start);
  573. BUG_ON(!ops->stop);
  574. BUG_ON(!ops->config);
  575. BUG_ON(!ops->add_interface);
  576. BUG_ON(!ops->remove_interface);
  577. BUG_ON(!ops->configure_filter);
  578. local->ops = ops;
  579. /* set up some defaults */
  580. local->hw.queues = 1;
  581. local->hw.max_rates = 1;
  582. local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
  583. local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
  584. local->hw.conf.radio_enabled = true;
  585. local->user_power_level = -1;
  586. INIT_LIST_HEAD(&local->interfaces);
  587. mutex_init(&local->iflist_mtx);
  588. mutex_init(&local->scan_mtx);
  589. spin_lock_init(&local->key_lock);
  590. spin_lock_init(&local->filter_lock);
  591. spin_lock_init(&local->queue_stop_reason_lock);
  592. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  593. INIT_WORK(&local->restart_work, ieee80211_restart_work);
  594. INIT_WORK(&local->dynamic_ps_enable_work,
  595. ieee80211_dynamic_ps_enable_work);
  596. INIT_WORK(&local->dynamic_ps_disable_work,
  597. ieee80211_dynamic_ps_disable_work);
  598. setup_timer(&local->dynamic_ps_timer,
  599. ieee80211_dynamic_ps_timer, (unsigned long) local);
  600. sta_info_init(local);
  601. for (i = 0; i < IEEE80211_MAX_QUEUES; i++)
  602. skb_queue_head_init(&local->pending[i]);
  603. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  604. (unsigned long)local);
  605. tasklet_disable(&local->tx_pending_tasklet);
  606. tasklet_init(&local->tasklet,
  607. ieee80211_tasklet_handler,
  608. (unsigned long) local);
  609. tasklet_disable(&local->tasklet);
  610. skb_queue_head_init(&local->skb_queue);
  611. skb_queue_head_init(&local->skb_queue_unreliable);
  612. spin_lock_init(&local->ampdu_lock);
  613. return local_to_hw(local);
  614. }
  615. EXPORT_SYMBOL(ieee80211_alloc_hw);
  616. int ieee80211_register_hw(struct ieee80211_hw *hw)
  617. {
  618. struct ieee80211_local *local = hw_to_local(hw);
  619. int result;
  620. enum ieee80211_band band;
  621. int channels, i, j, max_bitrates;
  622. bool supp_ht;
  623. static const u32 cipher_suites[] = {
  624. WLAN_CIPHER_SUITE_WEP40,
  625. WLAN_CIPHER_SUITE_WEP104,
  626. WLAN_CIPHER_SUITE_TKIP,
  627. WLAN_CIPHER_SUITE_CCMP,
  628. /* keep last -- depends on hw flags! */
  629. WLAN_CIPHER_SUITE_AES_CMAC
  630. };
  631. /*
  632. * generic code guarantees at least one band,
  633. * set this very early because much code assumes
  634. * that hw.conf.channel is assigned
  635. */
  636. channels = 0;
  637. max_bitrates = 0;
  638. supp_ht = false;
  639. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  640. struct ieee80211_supported_band *sband;
  641. sband = local->hw.wiphy->bands[band];
  642. if (!sband)
  643. continue;
  644. if (!local->oper_channel) {
  645. /* init channel we're on */
  646. local->hw.conf.channel =
  647. local->oper_channel = &sband->channels[0];
  648. local->hw.conf.channel_type = NL80211_CHAN_NO_HT;
  649. }
  650. channels += sband->n_channels;
  651. if (max_bitrates < sband->n_bitrates)
  652. max_bitrates = sband->n_bitrates;
  653. supp_ht = supp_ht || sband->ht_cap.ht_supported;
  654. }
  655. local->int_scan_req.n_channels = channels;
  656. local->int_scan_req.channels = kzalloc(sizeof(void *) * channels, GFP_KERNEL);
  657. if (!local->int_scan_req.channels)
  658. return -ENOMEM;
  659. /* if low-level driver supports AP, we also support VLAN */
  660. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  661. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  662. /* mac80211 always supports monitor */
  663. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  664. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  665. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  666. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  667. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  668. /*
  669. * Calculate scan IE length -- we need this to alloc
  670. * memory and to subtract from the driver limit. It
  671. * includes the (extended) supported rates and HT
  672. * information -- SSID is the driver's responsibility.
  673. */
  674. local->scan_ies_len = 4 + max_bitrates; /* (ext) supp rates */
  675. if (supp_ht)
  676. local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
  677. if (!local->ops->hw_scan) {
  678. /* For hw_scan, driver needs to set these up. */
  679. local->hw.wiphy->max_scan_ssids = 4;
  680. local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  681. }
  682. /*
  683. * If the driver supports any scan IEs, then assume the
  684. * limit includes the IEs mac80211 will add, otherwise
  685. * leave it at zero and let the driver sort it out; we
  686. * still pass our IEs to the driver but userspace will
  687. * not be allowed to in that case.
  688. */
  689. if (local->hw.wiphy->max_scan_ie_len)
  690. local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
  691. local->hw.wiphy->cipher_suites = cipher_suites;
  692. local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  693. if (!(local->hw.flags & IEEE80211_HW_MFP_CAPABLE))
  694. local->hw.wiphy->n_cipher_suites--;
  695. result = wiphy_register(local->hw.wiphy);
  696. if (result < 0)
  697. goto fail_wiphy_register;
  698. /*
  699. * We use the number of queues for feature tests (QoS, HT) internally
  700. * so restrict them appropriately.
  701. */
  702. if (hw->queues > IEEE80211_MAX_QUEUES)
  703. hw->queues = IEEE80211_MAX_QUEUES;
  704. local->hw.workqueue =
  705. create_singlethread_workqueue(wiphy_name(local->hw.wiphy));
  706. if (!local->hw.workqueue) {
  707. result = -ENOMEM;
  708. goto fail_workqueue;
  709. }
  710. /*
  711. * The hardware needs headroom for sending the frame,
  712. * and we need some headroom for passing the frame to monitor
  713. * interfaces, but never both at the same time.
  714. */
  715. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  716. sizeof(struct ieee80211_tx_status_rtap_hdr));
  717. debugfs_hw_add(local);
  718. if (local->hw.max_listen_interval == 0)
  719. local->hw.max_listen_interval = 1;
  720. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  721. result = sta_info_start(local);
  722. if (result < 0)
  723. goto fail_sta_info;
  724. result = ieee80211_wep_init(local);
  725. if (result < 0) {
  726. printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
  727. wiphy_name(local->hw.wiphy), result);
  728. goto fail_wep;
  729. }
  730. rtnl_lock();
  731. result = ieee80211_init_rate_ctrl_alg(local,
  732. hw->rate_control_algorithm);
  733. if (result < 0) {
  734. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  735. "algorithm\n", wiphy_name(local->hw.wiphy));
  736. goto fail_rate;
  737. }
  738. /* add one default STA interface if supported */
  739. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
  740. result = ieee80211_if_add(local, "wlan%d", NULL,
  741. NL80211_IFTYPE_STATION, NULL);
  742. if (result)
  743. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  744. wiphy_name(local->hw.wiphy));
  745. }
  746. rtnl_unlock();
  747. ieee80211_led_init(local);
  748. /* alloc internal scan request */
  749. i = 0;
  750. local->int_scan_req.ssids = &local->scan_ssid;
  751. local->int_scan_req.n_ssids = 1;
  752. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  753. if (!hw->wiphy->bands[band])
  754. continue;
  755. for (j = 0; j < hw->wiphy->bands[band]->n_channels; j++) {
  756. local->int_scan_req.channels[i] =
  757. &hw->wiphy->bands[band]->channels[j];
  758. i++;
  759. }
  760. }
  761. local->network_latency_notifier.notifier_call =
  762. ieee80211_max_network_latency;
  763. result = pm_qos_add_notifier(PM_QOS_NETWORK_LATENCY,
  764. &local->network_latency_notifier);
  765. if (result) {
  766. rtnl_lock();
  767. goto fail_pm_qos;
  768. }
  769. return 0;
  770. fail_pm_qos:
  771. ieee80211_led_exit(local);
  772. ieee80211_remove_interfaces(local);
  773. fail_rate:
  774. rtnl_unlock();
  775. ieee80211_wep_free(local);
  776. fail_wep:
  777. sta_info_stop(local);
  778. fail_sta_info:
  779. debugfs_hw_del(local);
  780. destroy_workqueue(local->hw.workqueue);
  781. fail_workqueue:
  782. wiphy_unregister(local->hw.wiphy);
  783. fail_wiphy_register:
  784. kfree(local->int_scan_req.channels);
  785. return result;
  786. }
  787. EXPORT_SYMBOL(ieee80211_register_hw);
  788. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  789. {
  790. struct ieee80211_local *local = hw_to_local(hw);
  791. tasklet_kill(&local->tx_pending_tasklet);
  792. tasklet_kill(&local->tasklet);
  793. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  794. &local->network_latency_notifier);
  795. rtnl_lock();
  796. /*
  797. * At this point, interface list manipulations are fine
  798. * because the driver cannot be handing us frames any
  799. * more and the tasklet is killed.
  800. */
  801. ieee80211_remove_interfaces(local);
  802. rtnl_unlock();
  803. ieee80211_clear_tx_pending(local);
  804. sta_info_stop(local);
  805. rate_control_deinitialize(local);
  806. debugfs_hw_del(local);
  807. if (skb_queue_len(&local->skb_queue)
  808. || skb_queue_len(&local->skb_queue_unreliable))
  809. printk(KERN_WARNING "%s: skb_queue not empty\n",
  810. wiphy_name(local->hw.wiphy));
  811. skb_queue_purge(&local->skb_queue);
  812. skb_queue_purge(&local->skb_queue_unreliable);
  813. destroy_workqueue(local->hw.workqueue);
  814. wiphy_unregister(local->hw.wiphy);
  815. ieee80211_wep_free(local);
  816. ieee80211_led_exit(local);
  817. kfree(local->int_scan_req.channels);
  818. }
  819. EXPORT_SYMBOL(ieee80211_unregister_hw);
  820. void ieee80211_free_hw(struct ieee80211_hw *hw)
  821. {
  822. struct ieee80211_local *local = hw_to_local(hw);
  823. mutex_destroy(&local->iflist_mtx);
  824. mutex_destroy(&local->scan_mtx);
  825. wiphy_free(local->hw.wiphy);
  826. }
  827. EXPORT_SYMBOL(ieee80211_free_hw);
  828. static int __init ieee80211_init(void)
  829. {
  830. struct sk_buff *skb;
  831. int ret;
  832. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  833. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  834. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  835. ret = rc80211_minstrel_init();
  836. if (ret)
  837. return ret;
  838. ret = rc80211_pid_init();
  839. if (ret)
  840. return ret;
  841. ieee80211_debugfs_netdev_init();
  842. return 0;
  843. }
  844. static void __exit ieee80211_exit(void)
  845. {
  846. rc80211_pid_exit();
  847. rc80211_minstrel_exit();
  848. /*
  849. * For key todo, it'll be empty by now but the work
  850. * might still be scheduled.
  851. */
  852. flush_scheduled_work();
  853. if (mesh_allocated)
  854. ieee80211s_stop();
  855. ieee80211_debugfs_netdev_exit();
  856. }
  857. subsys_initcall(ieee80211_init);
  858. module_exit(ieee80211_exit);
  859. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  860. MODULE_LICENSE("GPL");