main.c 31 KB

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