main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/bitmap.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include "ieee80211_i.h"
  26. #include "rate.h"
  27. #include "mesh.h"
  28. #include "wep.h"
  29. #include "wme.h"
  30. #include "aes_ccm.h"
  31. #include "led.h"
  32. #include "cfg.h"
  33. #include "debugfs.h"
  34. #include "debugfs_netdev.h"
  35. /*
  36. * For seeing transmitted packets on monitor interfaces
  37. * we have a radiotap header too.
  38. */
  39. struct ieee80211_tx_status_rtap_hdr {
  40. struct ieee80211_radiotap_header hdr;
  41. __le16 tx_flags;
  42. u8 data_retries;
  43. } __attribute__ ((packed));
  44. /* must be called under mdev tx lock */
  45. void ieee80211_configure_filter(struct ieee80211_local *local)
  46. {
  47. unsigned int changed_flags;
  48. unsigned int new_flags = 0;
  49. if (atomic_read(&local->iff_promiscs))
  50. new_flags |= FIF_PROMISC_IN_BSS;
  51. if (atomic_read(&local->iff_allmultis))
  52. new_flags |= FIF_ALLMULTI;
  53. if (local->monitors)
  54. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  55. if (local->fif_fcsfail)
  56. new_flags |= FIF_FCSFAIL;
  57. if (local->fif_plcpfail)
  58. new_flags |= FIF_PLCPFAIL;
  59. if (local->fif_control)
  60. new_flags |= FIF_CONTROL;
  61. if (local->fif_other_bss)
  62. new_flags |= FIF_OTHER_BSS;
  63. changed_flags = local->filter_flags ^ new_flags;
  64. /* be a bit nasty */
  65. new_flags |= (1<<31);
  66. local->ops->configure_filter(local_to_hw(local),
  67. changed_flags, &new_flags,
  68. local->mdev->mc_count,
  69. local->mdev->mc_list);
  70. WARN_ON(new_flags & (1<<31));
  71. local->filter_flags = new_flags & ~(1<<31);
  72. }
  73. /* master interface */
  74. static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
  75. {
  76. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  77. return ETH_ALEN;
  78. }
  79. static const struct header_ops ieee80211_header_ops = {
  80. .create = eth_header,
  81. .parse = header_parse_80211,
  82. .rebuild = eth_rebuild_header,
  83. .cache = eth_header_cache,
  84. .cache_update = eth_header_cache_update,
  85. };
  86. static int ieee80211_master_open(struct net_device *dev)
  87. {
  88. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  89. struct ieee80211_local *local = mpriv->local;
  90. struct ieee80211_sub_if_data *sdata;
  91. int res = -EOPNOTSUPP;
  92. /* we hold the RTNL here so can safely walk the list */
  93. list_for_each_entry(sdata, &local->interfaces, list) {
  94. if (netif_running(sdata->dev)) {
  95. res = 0;
  96. break;
  97. }
  98. }
  99. if (res)
  100. return res;
  101. netif_tx_start_all_queues(local->mdev);
  102. return 0;
  103. }
  104. static int ieee80211_master_stop(struct net_device *dev)
  105. {
  106. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  107. struct ieee80211_local *local = mpriv->local;
  108. struct ieee80211_sub_if_data *sdata;
  109. /* we hold the RTNL here so can safely walk the list */
  110. list_for_each_entry(sdata, &local->interfaces, list)
  111. if (netif_running(sdata->dev))
  112. dev_close(sdata->dev);
  113. return 0;
  114. }
  115. static void ieee80211_master_set_multicast_list(struct net_device *dev)
  116. {
  117. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  118. struct ieee80211_local *local = mpriv->local;
  119. ieee80211_configure_filter(local);
  120. }
  121. /* everything else */
  122. int ieee80211_if_config(struct ieee80211_sub_if_data *sdata, u32 changed)
  123. {
  124. struct ieee80211_local *local = sdata->local;
  125. struct ieee80211_if_conf conf;
  126. if (WARN_ON(!netif_running(sdata->dev)))
  127. return 0;
  128. if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  129. return -EINVAL;
  130. if (!local->ops->config_interface)
  131. return 0;
  132. memset(&conf, 0, sizeof(conf));
  133. conf.changed = changed;
  134. if (sdata->vif.type == NL80211_IFTYPE_STATION ||
  135. sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  136. conf.bssid = sdata->u.sta.bssid;
  137. conf.ssid = sdata->u.sta.ssid;
  138. conf.ssid_len = sdata->u.sta.ssid_len;
  139. } else if (sdata->vif.type == NL80211_IFTYPE_AP) {
  140. conf.bssid = sdata->dev->dev_addr;
  141. conf.ssid = sdata->u.ap.ssid;
  142. conf.ssid_len = sdata->u.ap.ssid_len;
  143. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  144. u8 zero[ETH_ALEN] = { 0 };
  145. conf.bssid = zero;
  146. conf.ssid = zero;
  147. conf.ssid_len = 0;
  148. } else {
  149. WARN_ON(1);
  150. return -EINVAL;
  151. }
  152. if (WARN_ON(!conf.bssid && (changed & IEEE80211_IFCC_BSSID)))
  153. return -EINVAL;
  154. if (WARN_ON(!conf.ssid && (changed & IEEE80211_IFCC_SSID)))
  155. return -EINVAL;
  156. return local->ops->config_interface(local_to_hw(local),
  157. &sdata->vif, &conf);
  158. }
  159. int ieee80211_hw_config(struct ieee80211_local *local)
  160. {
  161. struct ieee80211_channel *chan;
  162. int ret = 0;
  163. if (local->sw_scanning)
  164. chan = local->scan_channel;
  165. else
  166. chan = local->oper_channel;
  167. local->hw.conf.channel = chan;
  168. if (!local->hw.conf.power_level)
  169. local->hw.conf.power_level = chan->max_power;
  170. else
  171. local->hw.conf.power_level = min(chan->max_power,
  172. local->hw.conf.power_level);
  173. local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
  174. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  175. printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
  176. wiphy_name(local->hw.wiphy), chan->center_freq);
  177. #endif
  178. if (local->open_count)
  179. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  180. return ret;
  181. }
  182. /**
  183. * ieee80211_handle_ht should be used only after legacy configuration
  184. * has been determined namely band, as ht configuration depends upon
  185. * the hardware's HT abilities for a _specific_ band.
  186. */
  187. u32 ieee80211_handle_ht(struct ieee80211_local *local, int enable_ht,
  188. struct ieee80211_ht_info *req_ht_cap,
  189. struct ieee80211_ht_bss_info *req_bss_cap)
  190. {
  191. struct ieee80211_conf *conf = &local->hw.conf;
  192. struct ieee80211_supported_band *sband;
  193. struct ieee80211_ht_info ht_conf;
  194. struct ieee80211_ht_bss_info ht_bss_conf;
  195. u32 changed = 0;
  196. int i;
  197. u8 max_tx_streams = IEEE80211_HT_CAP_MAX_STREAMS;
  198. u8 tx_mcs_set_cap;
  199. sband = local->hw.wiphy->bands[conf->channel->band];
  200. memset(&ht_conf, 0, sizeof(struct ieee80211_ht_info));
  201. memset(&ht_bss_conf, 0, sizeof(struct ieee80211_ht_bss_info));
  202. /* HT is not supported */
  203. if (!sband->ht_info.ht_supported) {
  204. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  205. goto out;
  206. }
  207. /* disable HT */
  208. if (!enable_ht) {
  209. if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)
  210. changed |= BSS_CHANGED_HT;
  211. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  212. conf->ht_conf.ht_supported = 0;
  213. goto out;
  214. }
  215. if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE))
  216. changed |= BSS_CHANGED_HT;
  217. conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
  218. ht_conf.ht_supported = 1;
  219. ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
  220. ht_conf.cap &= ~(IEEE80211_HT_CAP_SM_PS);
  221. ht_conf.cap |= sband->ht_info.cap & IEEE80211_HT_CAP_SM_PS;
  222. ht_bss_conf.primary_channel = req_bss_cap->primary_channel;
  223. ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
  224. ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
  225. ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
  226. ht_conf.ampdu_density = req_ht_cap->ampdu_density;
  227. /* Bits 96-100 */
  228. tx_mcs_set_cap = sband->ht_info.supp_mcs_set[12];
  229. /* configure suppoerted Tx MCS according to requested MCS
  230. * (based in most cases on Rx capabilities of peer) and self
  231. * Tx MCS capabilities (as defined by low level driver HW
  232. * Tx capabilities) */
  233. if (!(tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_DEFINED))
  234. goto check_changed;
  235. /* Counting from 0 therfore + 1 */
  236. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_RX_DIFF)
  237. max_tx_streams = ((tx_mcs_set_cap &
  238. IEEE80211_HT_CAP_MCS_TX_STREAMS) >> 2) + 1;
  239. for (i = 0; i < max_tx_streams; i++)
  240. ht_conf.supp_mcs_set[i] =
  241. sband->ht_info.supp_mcs_set[i] &
  242. req_ht_cap->supp_mcs_set[i];
  243. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_UEQM)
  244. for (i = IEEE80211_SUPP_MCS_SET_UEQM;
  245. i < IEEE80211_SUPP_MCS_SET_LEN; i++)
  246. ht_conf.supp_mcs_set[i] =
  247. sband->ht_info.supp_mcs_set[i] &
  248. req_ht_cap->supp_mcs_set[i];
  249. check_changed:
  250. /* if bss configuration changed store the new one */
  251. if (memcmp(&conf->ht_conf, &ht_conf, sizeof(ht_conf)) ||
  252. memcmp(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf))) {
  253. changed |= BSS_CHANGED_HT;
  254. memcpy(&conf->ht_conf, &ht_conf, sizeof(ht_conf));
  255. memcpy(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf));
  256. }
  257. out:
  258. return changed;
  259. }
  260. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  261. u32 changed)
  262. {
  263. struct ieee80211_local *local = sdata->local;
  264. if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  265. return;
  266. if (!changed)
  267. return;
  268. if (local->ops->bss_info_changed)
  269. local->ops->bss_info_changed(local_to_hw(local),
  270. &sdata->vif,
  271. &sdata->bss_conf,
  272. changed);
  273. }
  274. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  275. {
  276. sdata->bss_conf.use_cts_prot = 0;
  277. sdata->bss_conf.use_short_preamble = 0;
  278. return BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_PREAMBLE;
  279. }
  280. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  281. struct sk_buff *skb)
  282. {
  283. struct ieee80211_local *local = hw_to_local(hw);
  284. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  285. int tmp;
  286. skb->dev = local->mdev;
  287. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  288. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  289. &local->skb_queue : &local->skb_queue_unreliable, skb);
  290. tmp = skb_queue_len(&local->skb_queue) +
  291. skb_queue_len(&local->skb_queue_unreliable);
  292. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  293. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  294. dev_kfree_skb_irq(skb);
  295. tmp--;
  296. I802_DEBUG_INC(local->tx_status_drop);
  297. }
  298. tasklet_schedule(&local->tasklet);
  299. }
  300. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  301. static void ieee80211_tasklet_handler(unsigned long data)
  302. {
  303. struct ieee80211_local *local = (struct ieee80211_local *) data;
  304. struct sk_buff *skb;
  305. struct ieee80211_rx_status rx_status;
  306. struct ieee80211_ra_tid *ra_tid;
  307. while ((skb = skb_dequeue(&local->skb_queue)) ||
  308. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  309. switch (skb->pkt_type) {
  310. case IEEE80211_RX_MSG:
  311. /* status is in skb->cb */
  312. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  313. /* Clear skb->pkt_type in order to not confuse kernel
  314. * netstack. */
  315. skb->pkt_type = 0;
  316. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  317. break;
  318. case IEEE80211_TX_STATUS_MSG:
  319. skb->pkt_type = 0;
  320. ieee80211_tx_status(local_to_hw(local), skb);
  321. break;
  322. case IEEE80211_DELBA_MSG:
  323. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  324. ieee80211_stop_tx_ba_cb(local_to_hw(local),
  325. ra_tid->ra, ra_tid->tid);
  326. dev_kfree_skb(skb);
  327. break;
  328. case IEEE80211_ADDBA_MSG:
  329. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  330. ieee80211_start_tx_ba_cb(local_to_hw(local),
  331. ra_tid->ra, ra_tid->tid);
  332. dev_kfree_skb(skb);
  333. break ;
  334. default:
  335. WARN_ON(1);
  336. dev_kfree_skb(skb);
  337. break;
  338. }
  339. }
  340. }
  341. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  342. * make a prepared TX frame (one that has been given to hw) to look like brand
  343. * new IEEE 802.11 frame that is ready to go through TX processing again.
  344. */
  345. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  346. struct ieee80211_key *key,
  347. struct sk_buff *skb)
  348. {
  349. unsigned int hdrlen, iv_len, mic_len;
  350. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  351. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  352. if (!key)
  353. goto no_key;
  354. switch (key->conf.alg) {
  355. case ALG_WEP:
  356. iv_len = WEP_IV_LEN;
  357. mic_len = WEP_ICV_LEN;
  358. break;
  359. case ALG_TKIP:
  360. iv_len = TKIP_IV_LEN;
  361. mic_len = TKIP_ICV_LEN;
  362. break;
  363. case ALG_CCMP:
  364. iv_len = CCMP_HDR_LEN;
  365. mic_len = CCMP_MIC_LEN;
  366. break;
  367. default:
  368. goto no_key;
  369. }
  370. if (skb->len >= hdrlen + mic_len &&
  371. !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  372. skb_trim(skb, skb->len - mic_len);
  373. if (skb->len >= hdrlen + iv_len) {
  374. memmove(skb->data + iv_len, skb->data, hdrlen);
  375. hdr = (struct ieee80211_hdr *)skb_pull(skb, iv_len);
  376. }
  377. no_key:
  378. if (ieee80211_is_data_qos(hdr->frame_control)) {
  379. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  380. memmove(skb->data + IEEE80211_QOS_CTL_LEN, skb->data,
  381. hdrlen - IEEE80211_QOS_CTL_LEN);
  382. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  383. }
  384. }
  385. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  386. struct sta_info *sta,
  387. struct sk_buff *skb)
  388. {
  389. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  390. sta->tx_filtered_count++;
  391. /*
  392. * Clear the TX filter mask for this STA when sending the next
  393. * packet. If the STA went to power save mode, this will happen
  394. * when it wakes up for the next time.
  395. */
  396. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  397. /*
  398. * This code races in the following way:
  399. *
  400. * (1) STA sends frame indicating it will go to sleep and does so
  401. * (2) hardware/firmware adds STA to filter list, passes frame up
  402. * (3) hardware/firmware processes TX fifo and suppresses a frame
  403. * (4) we get TX status before having processed the frame and
  404. * knowing that the STA has gone to sleep.
  405. *
  406. * This is actually quite unlikely even when both those events are
  407. * processed from interrupts coming in quickly after one another or
  408. * even at the same time because we queue both TX status events and
  409. * RX frames to be processed by a tasklet and process them in the
  410. * same order that they were received or TX status last. Hence, there
  411. * is no race as long as the frame RX is processed before the next TX
  412. * status, which drivers can ensure, see below.
  413. *
  414. * Note that this can only happen if the hardware or firmware can
  415. * actually add STAs to the filter list, if this is done by the
  416. * driver in response to set_tim() (which will only reduce the race
  417. * this whole filtering tries to solve, not completely solve it)
  418. * this situation cannot happen.
  419. *
  420. * To completely solve this race drivers need to make sure that they
  421. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  422. * functions and
  423. * (b) always process RX events before TX status events if ordering
  424. * can be unknown, for example with different interrupt status
  425. * bits.
  426. */
  427. if (test_sta_flags(sta, WLAN_STA_PS) &&
  428. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  429. ieee80211_remove_tx_extra(local, sta->key, skb);
  430. skb_queue_tail(&sta->tx_filtered, skb);
  431. return;
  432. }
  433. if (!test_sta_flags(sta, WLAN_STA_PS) &&
  434. !(info->flags & IEEE80211_TX_CTL_REQUEUE)) {
  435. /* Software retry the packet once */
  436. info->flags |= IEEE80211_TX_CTL_REQUEUE;
  437. ieee80211_remove_tx_extra(local, sta->key, skb);
  438. dev_queue_xmit(skb);
  439. return;
  440. }
  441. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  442. if (net_ratelimit())
  443. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  444. "queue_len=%d PS=%d @%lu\n",
  445. wiphy_name(local->hw.wiphy),
  446. skb_queue_len(&sta->tx_filtered),
  447. !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
  448. #endif
  449. dev_kfree_skb(skb);
  450. }
  451. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  452. {
  453. struct sk_buff *skb2;
  454. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  455. struct ieee80211_local *local = hw_to_local(hw);
  456. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  457. u16 frag, type;
  458. __le16 fc;
  459. struct ieee80211_supported_band *sband;
  460. struct ieee80211_tx_status_rtap_hdr *rthdr;
  461. struct ieee80211_sub_if_data *sdata;
  462. struct net_device *prev_dev = NULL;
  463. struct sta_info *sta;
  464. rcu_read_lock();
  465. sta = sta_info_get(local, hdr->addr1);
  466. if (sta) {
  467. if (info->status.excessive_retries &&
  468. test_sta_flags(sta, WLAN_STA_PS)) {
  469. /*
  470. * The STA is in power save mode, so assume
  471. * that this TX packet failed because of that.
  472. */
  473. ieee80211_handle_filtered_frame(local, sta, skb);
  474. rcu_read_unlock();
  475. return;
  476. }
  477. fc = hdr->frame_control;
  478. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  479. (ieee80211_is_data_qos(fc))) {
  480. u16 tid, ssn;
  481. u8 *qc;
  482. qc = ieee80211_get_qos_ctl(hdr);
  483. tid = qc[0] & 0xf;
  484. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  485. & IEEE80211_SCTL_SEQ);
  486. ieee80211_send_bar(sta->sdata, hdr->addr1,
  487. tid, ssn);
  488. }
  489. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  490. ieee80211_handle_filtered_frame(local, sta, skb);
  491. rcu_read_unlock();
  492. return;
  493. } else {
  494. if (info->status.excessive_retries)
  495. sta->tx_retry_failed++;
  496. sta->tx_retry_count += info->status.retry_count;
  497. }
  498. sband = local->hw.wiphy->bands[info->band];
  499. rate_control_tx_status(local, sband, sta, skb);
  500. }
  501. rcu_read_unlock();
  502. ieee80211_led_tx(local, 0);
  503. /* SNMP counters
  504. * Fragments are passed to low-level drivers as separate skbs, so these
  505. * are actually fragments, not frames. Update frame counters only for
  506. * the first fragment of the frame. */
  507. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  508. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  509. if (info->flags & IEEE80211_TX_STAT_ACK) {
  510. if (frag == 0) {
  511. local->dot11TransmittedFrameCount++;
  512. if (is_multicast_ether_addr(hdr->addr1))
  513. local->dot11MulticastTransmittedFrameCount++;
  514. if (info->status.retry_count > 0)
  515. local->dot11RetryCount++;
  516. if (info->status.retry_count > 1)
  517. local->dot11MultipleRetryCount++;
  518. }
  519. /* This counter shall be incremented for an acknowledged MPDU
  520. * with an individual address in the address 1 field or an MPDU
  521. * with a multicast address in the address 1 field of type Data
  522. * or Management. */
  523. if (!is_multicast_ether_addr(hdr->addr1) ||
  524. type == IEEE80211_FTYPE_DATA ||
  525. type == IEEE80211_FTYPE_MGMT)
  526. local->dot11TransmittedFragmentCount++;
  527. } else {
  528. if (frag == 0)
  529. local->dot11FailedCount++;
  530. }
  531. /* this was a transmitted frame, but now we want to reuse it */
  532. skb_orphan(skb);
  533. /*
  534. * This is a bit racy but we can avoid a lot of work
  535. * with this test...
  536. */
  537. if (!local->monitors && !local->cooked_mntrs) {
  538. dev_kfree_skb(skb);
  539. return;
  540. }
  541. /* send frame to monitor interfaces now */
  542. if (skb_headroom(skb) < sizeof(*rthdr)) {
  543. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  544. dev_kfree_skb(skb);
  545. return;
  546. }
  547. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  548. skb_push(skb, sizeof(*rthdr));
  549. memset(rthdr, 0, sizeof(*rthdr));
  550. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  551. rthdr->hdr.it_present =
  552. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  553. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  554. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  555. !is_multicast_ether_addr(hdr->addr1))
  556. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  557. if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) &&
  558. (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT))
  559. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  560. else if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  561. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  562. rthdr->data_retries = info->status.retry_count;
  563. /* XXX: is this sufficient for BPF? */
  564. skb_set_mac_header(skb, 0);
  565. skb->ip_summed = CHECKSUM_UNNECESSARY;
  566. skb->pkt_type = PACKET_OTHERHOST;
  567. skb->protocol = htons(ETH_P_802_2);
  568. memset(skb->cb, 0, sizeof(skb->cb));
  569. rcu_read_lock();
  570. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  571. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  572. if (!netif_running(sdata->dev))
  573. continue;
  574. if (prev_dev) {
  575. skb2 = skb_clone(skb, GFP_ATOMIC);
  576. if (skb2) {
  577. skb2->dev = prev_dev;
  578. netif_rx(skb2);
  579. }
  580. }
  581. prev_dev = sdata->dev;
  582. }
  583. }
  584. if (prev_dev) {
  585. skb->dev = prev_dev;
  586. netif_rx(skb);
  587. skb = NULL;
  588. }
  589. rcu_read_unlock();
  590. dev_kfree_skb(skb);
  591. }
  592. EXPORT_SYMBOL(ieee80211_tx_status);
  593. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  594. const struct ieee80211_ops *ops)
  595. {
  596. struct ieee80211_local *local;
  597. int priv_size;
  598. struct wiphy *wiphy;
  599. /* Ensure 32-byte alignment of our private data and hw private data.
  600. * We use the wiphy priv data for both our ieee80211_local and for
  601. * the driver's private data
  602. *
  603. * In memory it'll be like this:
  604. *
  605. * +-------------------------+
  606. * | struct wiphy |
  607. * +-------------------------+
  608. * | struct ieee80211_local |
  609. * +-------------------------+
  610. * | driver's private data |
  611. * +-------------------------+
  612. *
  613. */
  614. priv_size = ((sizeof(struct ieee80211_local) +
  615. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  616. priv_data_len;
  617. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  618. if (!wiphy)
  619. return NULL;
  620. wiphy->privid = mac80211_wiphy_privid;
  621. local = wiphy_priv(wiphy);
  622. local->hw.wiphy = wiphy;
  623. local->hw.priv = (char *)local +
  624. ((sizeof(struct ieee80211_local) +
  625. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  626. BUG_ON(!ops->tx);
  627. BUG_ON(!ops->start);
  628. BUG_ON(!ops->stop);
  629. BUG_ON(!ops->config);
  630. BUG_ON(!ops->add_interface);
  631. BUG_ON(!ops->remove_interface);
  632. BUG_ON(!ops->configure_filter);
  633. local->ops = ops;
  634. local->hw.queues = 1; /* default */
  635. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  636. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  637. local->short_retry_limit = 7;
  638. local->long_retry_limit = 4;
  639. local->hw.conf.radio_enabled = 1;
  640. INIT_LIST_HEAD(&local->interfaces);
  641. spin_lock_init(&local->key_lock);
  642. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  643. sta_info_init(local);
  644. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  645. (unsigned long)local);
  646. tasklet_disable(&local->tx_pending_tasklet);
  647. tasklet_init(&local->tasklet,
  648. ieee80211_tasklet_handler,
  649. (unsigned long) local);
  650. tasklet_disable(&local->tasklet);
  651. skb_queue_head_init(&local->skb_queue);
  652. skb_queue_head_init(&local->skb_queue_unreliable);
  653. return local_to_hw(local);
  654. }
  655. EXPORT_SYMBOL(ieee80211_alloc_hw);
  656. int ieee80211_register_hw(struct ieee80211_hw *hw)
  657. {
  658. struct ieee80211_local *local = hw_to_local(hw);
  659. const char *name;
  660. int result;
  661. enum ieee80211_band band;
  662. struct net_device *mdev;
  663. struct ieee80211_master_priv *mpriv;
  664. /*
  665. * generic code guarantees at least one band,
  666. * set this very early because much code assumes
  667. * that hw.conf.channel is assigned
  668. */
  669. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  670. struct ieee80211_supported_band *sband;
  671. sband = local->hw.wiphy->bands[band];
  672. if (sband) {
  673. /* init channel we're on */
  674. local->hw.conf.channel =
  675. local->oper_channel =
  676. local->scan_channel = &sband->channels[0];
  677. break;
  678. }
  679. }
  680. /* if low-level driver supports AP, we also support VLAN */
  681. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  682. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  683. /* mac80211 always supports monitor */
  684. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  685. result = wiphy_register(local->hw.wiphy);
  686. if (result < 0)
  687. return result;
  688. /*
  689. * We use the number of queues for feature tests (QoS, HT) internally
  690. * so restrict them appropriately.
  691. */
  692. if (hw->queues > IEEE80211_MAX_QUEUES)
  693. hw->queues = IEEE80211_MAX_QUEUES;
  694. if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
  695. hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
  696. if (hw->queues < 4)
  697. hw->ampdu_queues = 0;
  698. mdev = alloc_netdev_mq(sizeof(struct ieee80211_master_priv),
  699. "wmaster%d", ether_setup,
  700. ieee80211_num_queues(hw));
  701. if (!mdev)
  702. goto fail_mdev_alloc;
  703. mpriv = netdev_priv(mdev);
  704. mpriv->local = local;
  705. local->mdev = mdev;
  706. ieee80211_rx_bss_list_init(local);
  707. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  708. mdev->open = ieee80211_master_open;
  709. mdev->stop = ieee80211_master_stop;
  710. mdev->type = ARPHRD_IEEE80211;
  711. mdev->header_ops = &ieee80211_header_ops;
  712. mdev->set_multicast_list = ieee80211_master_set_multicast_list;
  713. name = wiphy_dev(local->hw.wiphy)->driver->name;
  714. local->hw.workqueue = create_freezeable_workqueue(name);
  715. if (!local->hw.workqueue) {
  716. result = -ENOMEM;
  717. goto fail_workqueue;
  718. }
  719. /*
  720. * The hardware needs headroom for sending the frame,
  721. * and we need some headroom for passing the frame to monitor
  722. * interfaces, but never both at the same time.
  723. */
  724. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  725. sizeof(struct ieee80211_tx_status_rtap_hdr));
  726. debugfs_hw_add(local);
  727. if (local->hw.conf.beacon_int < 10)
  728. local->hw.conf.beacon_int = 100;
  729. if (local->hw.max_listen_interval == 0)
  730. local->hw.max_listen_interval = 1;
  731. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  732. local->wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
  733. IEEE80211_HW_SIGNAL_DB |
  734. IEEE80211_HW_SIGNAL_DBM) ?
  735. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  736. local->wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
  737. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  738. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  739. local->wstats_flags |= IW_QUAL_DBM;
  740. result = sta_info_start(local);
  741. if (result < 0)
  742. goto fail_sta_info;
  743. rtnl_lock();
  744. result = dev_alloc_name(local->mdev, local->mdev->name);
  745. if (result < 0)
  746. goto fail_dev;
  747. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  748. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  749. result = register_netdevice(local->mdev);
  750. if (result < 0)
  751. goto fail_dev;
  752. result = ieee80211_init_rate_ctrl_alg(local,
  753. hw->rate_control_algorithm);
  754. if (result < 0) {
  755. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  756. "algorithm\n", wiphy_name(local->hw.wiphy));
  757. goto fail_rate;
  758. }
  759. result = ieee80211_wep_init(local);
  760. if (result < 0) {
  761. printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
  762. wiphy_name(local->hw.wiphy), result);
  763. goto fail_wep;
  764. }
  765. local->mdev->select_queue = ieee80211_select_queue;
  766. /* add one default STA interface */
  767. result = ieee80211_if_add(local, "wlan%d", NULL,
  768. NL80211_IFTYPE_STATION, NULL);
  769. if (result)
  770. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  771. wiphy_name(local->hw.wiphy));
  772. rtnl_unlock();
  773. ieee80211_led_init(local);
  774. return 0;
  775. fail_wep:
  776. rate_control_deinitialize(local);
  777. fail_rate:
  778. unregister_netdevice(local->mdev);
  779. local->mdev = NULL;
  780. fail_dev:
  781. rtnl_unlock();
  782. sta_info_stop(local);
  783. fail_sta_info:
  784. debugfs_hw_del(local);
  785. destroy_workqueue(local->hw.workqueue);
  786. fail_workqueue:
  787. if (local->mdev)
  788. free_netdev(local->mdev);
  789. fail_mdev_alloc:
  790. wiphy_unregister(local->hw.wiphy);
  791. return result;
  792. }
  793. EXPORT_SYMBOL(ieee80211_register_hw);
  794. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  795. {
  796. struct ieee80211_local *local = hw_to_local(hw);
  797. tasklet_kill(&local->tx_pending_tasklet);
  798. tasklet_kill(&local->tasklet);
  799. rtnl_lock();
  800. /*
  801. * At this point, interface list manipulations are fine
  802. * because the driver cannot be handing us frames any
  803. * more and the tasklet is killed.
  804. */
  805. /* First, we remove all virtual interfaces. */
  806. ieee80211_remove_interfaces(local);
  807. /* then, finally, remove the master interface */
  808. unregister_netdevice(local->mdev);
  809. rtnl_unlock();
  810. ieee80211_rx_bss_list_deinit(local);
  811. ieee80211_clear_tx_pending(local);
  812. sta_info_stop(local);
  813. rate_control_deinitialize(local);
  814. debugfs_hw_del(local);
  815. if (skb_queue_len(&local->skb_queue)
  816. || skb_queue_len(&local->skb_queue_unreliable))
  817. printk(KERN_WARNING "%s: skb_queue not empty\n",
  818. wiphy_name(local->hw.wiphy));
  819. skb_queue_purge(&local->skb_queue);
  820. skb_queue_purge(&local->skb_queue_unreliable);
  821. destroy_workqueue(local->hw.workqueue);
  822. wiphy_unregister(local->hw.wiphy);
  823. ieee80211_wep_free(local);
  824. ieee80211_led_exit(local);
  825. free_netdev(local->mdev);
  826. }
  827. EXPORT_SYMBOL(ieee80211_unregister_hw);
  828. void ieee80211_free_hw(struct ieee80211_hw *hw)
  829. {
  830. struct ieee80211_local *local = hw_to_local(hw);
  831. wiphy_free(local->hw.wiphy);
  832. }
  833. EXPORT_SYMBOL(ieee80211_free_hw);
  834. static int __init ieee80211_init(void)
  835. {
  836. struct sk_buff *skb;
  837. int ret;
  838. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  839. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  840. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  841. ret = rc80211_minstrel_init();
  842. if (ret)
  843. return ret;
  844. ret = rc80211_pid_init();
  845. if (ret)
  846. return ret;
  847. ieee80211_debugfs_netdev_init();
  848. return 0;
  849. }
  850. static void __exit ieee80211_exit(void)
  851. {
  852. rc80211_pid_exit();
  853. rc80211_minstrel_exit();
  854. /*
  855. * For key todo, it'll be empty by now but the work
  856. * might still be scheduled.
  857. */
  858. flush_scheduled_work();
  859. if (mesh_allocated)
  860. ieee80211s_stop();
  861. ieee80211_debugfs_netdev_exit();
  862. }
  863. subsys_initcall(ieee80211_init);
  864. module_exit(ieee80211_exit);
  865. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  866. MODULE_LICENSE("GPL");