main.c 29 KB

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