main.c 27 KB

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