main.c 27 KB

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