main.c 31 KB

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