main.c 30 KB

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