main.c 25 KB

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