PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/net/mac80211/mlme.c

https://bitbucket.org/abioy/linux
C | 2141 lines | 1516 code | 392 blank | 233 comment | 279 complexity | 28f4deaceab71fbdc5f5a780a0e81374 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/pm_qos_params.h>
  20. #include <linux/crc32.h>
  21. #include <linux/slab.h>
  22. #include <net/mac80211.h>
  23. #include <asm/unaligned.h>
  24. #include "ieee80211_i.h"
  25. #include "driver-ops.h"
  26. #include "rate.h"
  27. #include "led.h"
  28. #define IEEE80211_MAX_PROBE_TRIES 5
  29. /*
  30. * beacon loss detection timeout
  31. * XXX: should depend on beacon interval
  32. */
  33. #define IEEE80211_BEACON_LOSS_TIME (2 * HZ)
  34. /*
  35. * Time the connection can be idle before we probe
  36. * it to see if we can still talk to the AP.
  37. */
  38. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  39. /*
  40. * Time we wait for a probe response after sending
  41. * a probe request because of beacon loss or for
  42. * checking the connection still works.
  43. */
  44. #define IEEE80211_PROBE_WAIT (HZ / 2)
  45. #define TMR_RUNNING_TIMER 0
  46. #define TMR_RUNNING_CHANSW 1
  47. /*
  48. * All cfg80211 functions have to be called outside a locked
  49. * section so that they can acquire a lock themselves... This
  50. * is much simpler than queuing up things in cfg80211, but we
  51. * do need some indirection for that here.
  52. */
  53. enum rx_mgmt_action {
  54. /* no action required */
  55. RX_MGMT_NONE,
  56. /* caller must call cfg80211_send_rx_auth() */
  57. RX_MGMT_CFG80211_AUTH,
  58. /* caller must call cfg80211_send_rx_assoc() */
  59. RX_MGMT_CFG80211_ASSOC,
  60. /* caller must call cfg80211_send_deauth() */
  61. RX_MGMT_CFG80211_DEAUTH,
  62. /* caller must call cfg80211_send_disassoc() */
  63. RX_MGMT_CFG80211_DISASSOC,
  64. /* caller must tell cfg80211 about internal error */
  65. RX_MGMT_CFG80211_ASSOC_ERROR,
  66. };
  67. /* utils */
  68. static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
  69. {
  70. WARN_ON(!mutex_is_locked(&ifmgd->mtx));
  71. }
  72. /*
  73. * We can have multiple work items (and connection probing)
  74. * scheduling this timer, but we need to take care to only
  75. * reschedule it when it should fire _earlier_ than it was
  76. * asked for before, or if it's not pending right now. This
  77. * function ensures that. Note that it then is required to
  78. * run this function for all timeouts after the first one
  79. * has happened -- the work that runs from this timer will
  80. * do that.
  81. */
  82. static void run_again(struct ieee80211_if_managed *ifmgd,
  83. unsigned long timeout)
  84. {
  85. ASSERT_MGD_MTX(ifmgd);
  86. if (!timer_pending(&ifmgd->timer) ||
  87. time_before(timeout, ifmgd->timer.expires))
  88. mod_timer(&ifmgd->timer, timeout);
  89. }
  90. static void mod_beacon_timer(struct ieee80211_sub_if_data *sdata)
  91. {
  92. if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
  93. return;
  94. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  95. round_jiffies_up(jiffies + IEEE80211_BEACON_LOSS_TIME));
  96. }
  97. static int ecw2cw(int ecw)
  98. {
  99. return (1 << ecw) - 1;
  100. }
  101. /*
  102. * ieee80211_enable_ht should be called only after the operating band
  103. * has been determined as ht configuration depends on the hw's
  104. * HT abilities for a specific band.
  105. */
  106. static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
  107. struct ieee80211_ht_info *hti,
  108. const u8 *bssid, u16 ap_ht_cap_flags)
  109. {
  110. struct ieee80211_local *local = sdata->local;
  111. struct ieee80211_supported_band *sband;
  112. struct sta_info *sta;
  113. u32 changed = 0;
  114. u16 ht_opmode;
  115. bool enable_ht = true, ht_changed;
  116. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  117. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  118. /* HT is not supported */
  119. if (!sband->ht_cap.ht_supported)
  120. enable_ht = false;
  121. /* check that channel matches the right operating channel */
  122. if (local->hw.conf.channel->center_freq !=
  123. ieee80211_channel_to_frequency(hti->control_chan))
  124. enable_ht = false;
  125. if (enable_ht) {
  126. channel_type = NL80211_CHAN_HT20;
  127. if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
  128. (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
  129. (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  130. switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  131. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  132. if (!(local->hw.conf.channel->flags &
  133. IEEE80211_CHAN_NO_HT40PLUS))
  134. channel_type = NL80211_CHAN_HT40PLUS;
  135. break;
  136. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  137. if (!(local->hw.conf.channel->flags &
  138. IEEE80211_CHAN_NO_HT40MINUS))
  139. channel_type = NL80211_CHAN_HT40MINUS;
  140. break;
  141. }
  142. }
  143. }
  144. ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
  145. channel_type != local->hw.conf.channel_type;
  146. if (local->tmp_channel)
  147. local->tmp_channel_type = channel_type;
  148. local->oper_channel_type = channel_type;
  149. if (ht_changed) {
  150. /* channel_type change automatically detected */
  151. ieee80211_hw_config(local, 0);
  152. rcu_read_lock();
  153. sta = sta_info_get(sdata, bssid);
  154. if (sta)
  155. rate_control_rate_update(local, sband, sta,
  156. IEEE80211_RC_HT_CHANGED,
  157. local->oper_channel_type);
  158. rcu_read_unlock();
  159. }
  160. /* disable HT */
  161. if (!enable_ht)
  162. return 0;
  163. ht_opmode = le16_to_cpu(hti->operation_mode);
  164. /* if bss configuration changed store the new one */
  165. if (!sdata->ht_opmode_valid ||
  166. sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
  167. changed |= BSS_CHANGED_HT;
  168. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  169. sdata->ht_opmode_valid = true;
  170. }
  171. return changed;
  172. }
  173. /* frame sending functions */
  174. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  175. const u8 *bssid, u16 stype, u16 reason,
  176. void *cookie)
  177. {
  178. struct ieee80211_local *local = sdata->local;
  179. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  180. struct sk_buff *skb;
  181. struct ieee80211_mgmt *mgmt;
  182. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  183. if (!skb) {
  184. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  185. "deauth/disassoc frame\n", sdata->name);
  186. return;
  187. }
  188. skb_reserve(skb, local->hw.extra_tx_headroom);
  189. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  190. memset(mgmt, 0, 24);
  191. memcpy(mgmt->da, bssid, ETH_ALEN);
  192. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  193. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  194. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  195. skb_put(skb, 2);
  196. /* u.deauth.reason_code == u.disassoc.reason_code */
  197. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  198. if (stype == IEEE80211_STYPE_DEAUTH)
  199. if (cookie)
  200. __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  201. else
  202. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  203. else
  204. if (cookie)
  205. __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  206. else
  207. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  208. if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
  209. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  210. ieee80211_tx_skb(sdata, skb);
  211. }
  212. void ieee80211_send_pspoll(struct ieee80211_local *local,
  213. struct ieee80211_sub_if_data *sdata)
  214. {
  215. struct ieee80211_pspoll *pspoll;
  216. struct sk_buff *skb;
  217. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  218. if (!skb)
  219. return;
  220. pspoll = (struct ieee80211_pspoll *) skb->data;
  221. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  222. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  223. ieee80211_tx_skb(sdata, skb);
  224. }
  225. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  226. struct ieee80211_sub_if_data *sdata,
  227. int powersave)
  228. {
  229. struct sk_buff *skb;
  230. struct ieee80211_hdr_3addr *nullfunc;
  231. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  232. if (!skb)
  233. return;
  234. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  235. if (powersave)
  236. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  237. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  238. ieee80211_tx_skb(sdata, skb);
  239. }
  240. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  241. struct ieee80211_sub_if_data *sdata)
  242. {
  243. struct sk_buff *skb;
  244. struct ieee80211_hdr *nullfunc;
  245. __le16 fc;
  246. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  247. return;
  248. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  249. if (!skb) {
  250. printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
  251. "nullfunc frame\n", sdata->name);
  252. return;
  253. }
  254. skb_reserve(skb, local->hw.extra_tx_headroom);
  255. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  256. memset(nullfunc, 0, 30);
  257. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  258. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  259. nullfunc->frame_control = fc;
  260. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  261. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  262. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  263. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  264. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  265. ieee80211_tx_skb(sdata, skb);
  266. }
  267. /* spectrum management related things */
  268. static void ieee80211_chswitch_work(struct work_struct *work)
  269. {
  270. struct ieee80211_sub_if_data *sdata =
  271. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  272. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  273. if (!ieee80211_sdata_running(sdata))
  274. return;
  275. mutex_lock(&ifmgd->mtx);
  276. if (!ifmgd->associated)
  277. goto out;
  278. sdata->local->oper_channel = sdata->local->csa_channel;
  279. ieee80211_hw_config(sdata->local, IEEE80211_CONF_CHANGE_CHANNEL);
  280. /* XXX: shouldn't really modify cfg80211-owned data! */
  281. ifmgd->associated->channel = sdata->local->oper_channel;
  282. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  283. IEEE80211_QUEUE_STOP_REASON_CSA);
  284. out:
  285. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  286. mutex_unlock(&ifmgd->mtx);
  287. }
  288. static void ieee80211_chswitch_timer(unsigned long data)
  289. {
  290. struct ieee80211_sub_if_data *sdata =
  291. (struct ieee80211_sub_if_data *) data;
  292. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  293. if (sdata->local->quiescing) {
  294. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  295. return;
  296. }
  297. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  298. }
  299. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  300. struct ieee80211_channel_sw_ie *sw_elem,
  301. struct ieee80211_bss *bss)
  302. {
  303. struct cfg80211_bss *cbss =
  304. container_of((void *)bss, struct cfg80211_bss, priv);
  305. struct ieee80211_channel *new_ch;
  306. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  307. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
  308. ASSERT_MGD_MTX(ifmgd);
  309. if (!ifmgd->associated)
  310. return;
  311. if (sdata->local->scanning)
  312. return;
  313. /* Disregard subsequent beacons if we are already running a timer
  314. processing a CSA */
  315. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  316. return;
  317. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  318. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
  319. return;
  320. sdata->local->csa_channel = new_ch;
  321. if (sw_elem->count <= 1) {
  322. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  323. } else {
  324. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  325. IEEE80211_QUEUE_STOP_REASON_CSA);
  326. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  327. mod_timer(&ifmgd->chswitch_timer,
  328. jiffies +
  329. msecs_to_jiffies(sw_elem->count *
  330. cbss->beacon_interval));
  331. }
  332. }
  333. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  334. u16 capab_info, u8 *pwr_constr_elem,
  335. u8 pwr_constr_elem_len)
  336. {
  337. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  338. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  339. return;
  340. /* Power constraint IE length should be 1 octet */
  341. if (pwr_constr_elem_len != 1)
  342. return;
  343. if ((*pwr_constr_elem <= conf->channel->max_power) &&
  344. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  345. sdata->local->power_constr_level = *pwr_constr_elem;
  346. ieee80211_hw_config(sdata->local, 0);
  347. }
  348. }
  349. /* powersave */
  350. static void ieee80211_enable_ps(struct ieee80211_local *local,
  351. struct ieee80211_sub_if_data *sdata)
  352. {
  353. struct ieee80211_conf *conf = &local->hw.conf;
  354. /*
  355. * If we are scanning right now then the parameters will
  356. * take effect when scan finishes.
  357. */
  358. if (local->scanning)
  359. return;
  360. if (conf->dynamic_ps_timeout > 0 &&
  361. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  362. mod_timer(&local->dynamic_ps_timer, jiffies +
  363. msecs_to_jiffies(conf->dynamic_ps_timeout));
  364. } else {
  365. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  366. ieee80211_send_nullfunc(local, sdata, 1);
  367. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  368. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  369. return;
  370. conf->flags |= IEEE80211_CONF_PS;
  371. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  372. }
  373. }
  374. static void ieee80211_change_ps(struct ieee80211_local *local)
  375. {
  376. struct ieee80211_conf *conf = &local->hw.conf;
  377. if (local->ps_sdata) {
  378. ieee80211_enable_ps(local, local->ps_sdata);
  379. } else if (conf->flags & IEEE80211_CONF_PS) {
  380. conf->flags &= ~IEEE80211_CONF_PS;
  381. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  382. del_timer_sync(&local->dynamic_ps_timer);
  383. cancel_work_sync(&local->dynamic_ps_enable_work);
  384. }
  385. }
  386. /* need to hold RTNL or interface lock */
  387. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  388. {
  389. struct ieee80211_sub_if_data *sdata, *found = NULL;
  390. int count = 0;
  391. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  392. local->ps_sdata = NULL;
  393. return;
  394. }
  395. if (!list_empty(&local->work_list)) {
  396. local->ps_sdata = NULL;
  397. goto change;
  398. }
  399. list_for_each_entry(sdata, &local->interfaces, list) {
  400. if (!ieee80211_sdata_running(sdata))
  401. continue;
  402. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  403. continue;
  404. found = sdata;
  405. count++;
  406. }
  407. if (count == 1 && found->u.mgd.powersave &&
  408. found->u.mgd.associated &&
  409. found->u.mgd.associated->beacon_ies &&
  410. !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
  411. IEEE80211_STA_CONNECTION_POLL))) {
  412. s32 beaconint_us;
  413. if (latency < 0)
  414. latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
  415. beaconint_us = ieee80211_tu_to_usec(
  416. found->vif.bss_conf.beacon_int);
  417. if (beaconint_us > latency) {
  418. local->ps_sdata = NULL;
  419. } else {
  420. struct ieee80211_bss *bss;
  421. int maxslp = 1;
  422. u8 dtimper;
  423. bss = (void *)found->u.mgd.associated->priv;
  424. dtimper = bss->dtim_period;
  425. /* If the TIM IE is invalid, pretend the value is 1 */
  426. if (!dtimper)
  427. dtimper = 1;
  428. else if (dtimper > 1)
  429. maxslp = min_t(int, dtimper,
  430. latency / beaconint_us);
  431. local->hw.conf.max_sleep_period = maxslp;
  432. local->hw.conf.ps_dtim_period = dtimper;
  433. local->ps_sdata = found;
  434. }
  435. } else {
  436. local->ps_sdata = NULL;
  437. }
  438. change:
  439. ieee80211_change_ps(local);
  440. }
  441. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  442. {
  443. struct ieee80211_local *local =
  444. container_of(work, struct ieee80211_local,
  445. dynamic_ps_disable_work);
  446. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  447. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  448. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  449. }
  450. ieee80211_wake_queues_by_reason(&local->hw,
  451. IEEE80211_QUEUE_STOP_REASON_PS);
  452. }
  453. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  454. {
  455. struct ieee80211_local *local =
  456. container_of(work, struct ieee80211_local,
  457. dynamic_ps_enable_work);
  458. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  459. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  460. /* can only happen when PS was just disabled anyway */
  461. if (!sdata)
  462. return;
  463. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  464. return;
  465. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  466. (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
  467. ieee80211_send_nullfunc(local, sdata, 1);
  468. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  469. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  470. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  471. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  472. local->hw.conf.flags |= IEEE80211_CONF_PS;
  473. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  474. }
  475. }
  476. void ieee80211_dynamic_ps_timer(unsigned long data)
  477. {
  478. struct ieee80211_local *local = (void *) data;
  479. if (local->quiescing || local->suspended)
  480. return;
  481. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  482. }
  483. /* MLME */
  484. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  485. struct ieee80211_if_managed *ifmgd,
  486. u8 *wmm_param, size_t wmm_param_len)
  487. {
  488. struct ieee80211_tx_queue_params params;
  489. size_t left;
  490. int count;
  491. u8 *pos, uapsd_queues = 0;
  492. if (local->hw.queues < 4)
  493. return;
  494. if (!wmm_param)
  495. return;
  496. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  497. return;
  498. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  499. uapsd_queues = local->uapsd_queues;
  500. count = wmm_param[6] & 0x0f;
  501. if (count == ifmgd->wmm_last_param_set)
  502. return;
  503. ifmgd->wmm_last_param_set = count;
  504. pos = wmm_param + 8;
  505. left = wmm_param_len - 8;
  506. memset(&params, 0, sizeof(params));
  507. local->wmm_acm = 0;
  508. for (; left >= 4; left -= 4, pos += 4) {
  509. int aci = (pos[0] >> 5) & 0x03;
  510. int acm = (pos[0] >> 4) & 0x01;
  511. bool uapsd = false;
  512. int queue;
  513. switch (aci) {
  514. case 1: /* AC_BK */
  515. queue = 3;
  516. if (acm)
  517. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  518. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  519. uapsd = true;
  520. break;
  521. case 2: /* AC_VI */
  522. queue = 1;
  523. if (acm)
  524. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  525. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  526. uapsd = true;
  527. break;
  528. case 3: /* AC_VO */
  529. queue = 0;
  530. if (acm)
  531. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  532. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  533. uapsd = true;
  534. break;
  535. case 0: /* AC_BE */
  536. default:
  537. queue = 2;
  538. if (acm)
  539. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  540. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  541. uapsd = true;
  542. break;
  543. }
  544. params.aifs = pos[0] & 0x0f;
  545. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  546. params.cw_min = ecw2cw(pos[1] & 0x0f);
  547. params.txop = get_unaligned_le16(pos + 2);
  548. params.uapsd = uapsd;
  549. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  550. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  551. "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  552. wiphy_name(local->hw.wiphy), queue, aci, acm,
  553. params.aifs, params.cw_min, params.cw_max, params.txop,
  554. params.uapsd);
  555. #endif
  556. if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
  557. printk(KERN_DEBUG "%s: failed to set TX queue "
  558. "parameters for queue %d\n",
  559. wiphy_name(local->hw.wiphy), queue);
  560. }
  561. }
  562. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  563. u16 capab, bool erp_valid, u8 erp)
  564. {
  565. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  566. u32 changed = 0;
  567. bool use_protection;
  568. bool use_short_preamble;
  569. bool use_short_slot;
  570. if (erp_valid) {
  571. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  572. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  573. } else {
  574. use_protection = false;
  575. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  576. }
  577. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  578. if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
  579. use_short_slot = true;
  580. if (use_protection != bss_conf->use_cts_prot) {
  581. bss_conf->use_cts_prot = use_protection;
  582. changed |= BSS_CHANGED_ERP_CTS_PROT;
  583. }
  584. if (use_short_preamble != bss_conf->use_short_preamble) {
  585. bss_conf->use_short_preamble = use_short_preamble;
  586. changed |= BSS_CHANGED_ERP_PREAMBLE;
  587. }
  588. if (use_short_slot != bss_conf->use_short_slot) {
  589. bss_conf->use_short_slot = use_short_slot;
  590. changed |= BSS_CHANGED_ERP_SLOT;
  591. }
  592. return changed;
  593. }
  594. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  595. struct cfg80211_bss *cbss,
  596. u32 bss_info_changed)
  597. {
  598. struct ieee80211_bss *bss = (void *)cbss->priv;
  599. struct ieee80211_local *local = sdata->local;
  600. bss_info_changed |= BSS_CHANGED_ASSOC;
  601. /* set timing information */
  602. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  603. sdata->vif.bss_conf.timestamp = cbss->tsf;
  604. bss_info_changed |= BSS_CHANGED_BEACON_INT;
  605. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  606. cbss->capability, bss->has_erp_value, bss->erp_value);
  607. sdata->u.mgd.associated = cbss;
  608. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  609. /* just to be sure */
  610. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  611. IEEE80211_STA_BEACON_POLL);
  612. /*
  613. * Always handle WMM once after association regardless
  614. * of the first value the AP uses. Setting -1 here has
  615. * that effect because the AP values is an unsigned
  616. * 4-bit value.
  617. */
  618. sdata->u.mgd.wmm_last_param_set = -1;
  619. ieee80211_led_assoc(local, 1);
  620. sdata->vif.bss_conf.assoc = 1;
  621. /*
  622. * For now just always ask the driver to update the basic rateset
  623. * when we have associated, we aren't checking whether it actually
  624. * changed or not.
  625. */
  626. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  627. /* And the BSSID changed - we're associated now */
  628. bss_info_changed |= BSS_CHANGED_BSSID;
  629. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  630. mutex_lock(&local->iflist_mtx);
  631. ieee80211_recalc_ps(local, -1);
  632. ieee80211_recalc_smps(local, sdata);
  633. mutex_unlock(&local->iflist_mtx);
  634. netif_tx_start_all_queues(sdata->dev);
  635. netif_carrier_on(sdata->dev);
  636. }
  637. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
  638. {
  639. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  640. struct ieee80211_local *local = sdata->local;
  641. struct sta_info *sta;
  642. u32 changed = 0, config_changed = 0;
  643. u8 bssid[ETH_ALEN];
  644. ASSERT_MGD_MTX(ifmgd);
  645. if (WARN_ON(!ifmgd->associated))
  646. return;
  647. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  648. ifmgd->associated = NULL;
  649. memset(ifmgd->bssid, 0, ETH_ALEN);
  650. /*
  651. * we need to commit the associated = NULL change because the
  652. * scan code uses that to determine whether this iface should
  653. * go to/wake up from powersave or not -- and could otherwise
  654. * wake the queues erroneously.
  655. */
  656. smp_mb();
  657. /*
  658. * Thus, we can only afterwards stop the queues -- to account
  659. * for the case where another CPU is finishing a scan at this
  660. * time -- we don't want the scan code to enable queues.
  661. */
  662. netif_tx_stop_all_queues(sdata->dev);
  663. netif_carrier_off(sdata->dev);
  664. rcu_read_lock();
  665. sta = sta_info_get(sdata, bssid);
  666. if (sta) {
  667. set_sta_flags(sta, WLAN_STA_DISASSOC);
  668. ieee80211_sta_tear_down_BA_sessions(sta);
  669. }
  670. rcu_read_unlock();
  671. changed |= ieee80211_reset_erp_info(sdata);
  672. ieee80211_led_assoc(local, 0);
  673. changed |= BSS_CHANGED_ASSOC;
  674. sdata->vif.bss_conf.assoc = false;
  675. ieee80211_set_wmm_default(sdata);
  676. /* channel(_type) changes are handled by ieee80211_hw_config */
  677. local->oper_channel_type = NL80211_CHAN_NO_HT;
  678. /* on the next assoc, re-program HT parameters */
  679. sdata->ht_opmode_valid = false;
  680. local->power_constr_level = 0;
  681. del_timer_sync(&local->dynamic_ps_timer);
  682. cancel_work_sync(&local->dynamic_ps_enable_work);
  683. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  684. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  685. config_changed |= IEEE80211_CONF_CHANGE_PS;
  686. }
  687. ieee80211_hw_config(local, config_changed);
  688. /* And the BSSID changed -- not very interesting here */
  689. changed |= BSS_CHANGED_BSSID;
  690. ieee80211_bss_info_change_notify(sdata, changed);
  691. sta_info_destroy_addr(sdata, bssid);
  692. }
  693. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  694. struct ieee80211_hdr *hdr)
  695. {
  696. /*
  697. * We can postpone the mgd.timer whenever receiving unicast frames
  698. * from AP because we know that the connection is working both ways
  699. * at that time. But multicast frames (and hence also beacons) must
  700. * be ignored here, because we need to trigger the timer during
  701. * data idle periods for sending the periodic probe request to the
  702. * AP we're connected to.
  703. */
  704. if (is_multicast_ether_addr(hdr->addr1))
  705. return;
  706. mod_timer(&sdata->u.mgd.conn_mon_timer,
  707. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  708. }
  709. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  710. {
  711. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  712. const u8 *ssid;
  713. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  714. ieee80211_send_probe_req(sdata, ifmgd->associated->bssid,
  715. ssid + 2, ssid[1], NULL, 0);
  716. ifmgd->probe_send_count++;
  717. ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
  718. run_again(ifmgd, ifmgd->probe_timeout);
  719. }
  720. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  721. bool beacon)
  722. {
  723. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  724. bool already = false;
  725. if (!ieee80211_sdata_running(sdata))
  726. return;
  727. if (sdata->local->scanning)
  728. return;
  729. if (sdata->local->tmp_channel)
  730. return;
  731. mutex_lock(&ifmgd->mtx);
  732. if (!ifmgd->associated)
  733. goto out;
  734. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  735. if (beacon && net_ratelimit())
  736. printk(KERN_DEBUG "%s: detected beacon loss from AP "
  737. "- sending probe request\n", sdata->name);
  738. #endif
  739. /*
  740. * The driver/our work has already reported this event or the
  741. * connection monitoring has kicked in and we have already sent
  742. * a probe request. Or maybe the AP died and the driver keeps
  743. * reporting until we disassociate...
  744. *
  745. * In either case we have to ignore the current call to this
  746. * function (except for setting the correct probe reason bit)
  747. * because otherwise we would reset the timer every time and
  748. * never check whether we received a probe response!
  749. */
  750. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  751. IEEE80211_STA_CONNECTION_POLL))
  752. already = true;
  753. if (beacon)
  754. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  755. else
  756. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  757. if (already)
  758. goto out;
  759. mutex_lock(&sdata->local->iflist_mtx);
  760. ieee80211_recalc_ps(sdata->local, -1);
  761. mutex_unlock(&sdata->local->iflist_mtx);
  762. ifmgd->probe_send_count = 0;
  763. ieee80211_mgd_probe_ap_send(sdata);
  764. out:
  765. mutex_unlock(&ifmgd->mtx);
  766. }
  767. void ieee80211_beacon_loss_work(struct work_struct *work)
  768. {
  769. struct ieee80211_sub_if_data *sdata =
  770. container_of(work, struct ieee80211_sub_if_data,
  771. u.mgd.beacon_loss_work);
  772. ieee80211_mgd_probe_ap(sdata, true);
  773. }
  774. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  775. {
  776. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  777. ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
  778. }
  779. EXPORT_SYMBOL(ieee80211_beacon_loss);
  780. static enum rx_mgmt_action __must_check
  781. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  782. struct ieee80211_mgmt *mgmt, size_t len)
  783. {
  784. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  785. const u8 *bssid = NULL;
  786. u16 reason_code;
  787. if (len < 24 + 2)
  788. return RX_MGMT_NONE;
  789. ASSERT_MGD_MTX(ifmgd);
  790. bssid = ifmgd->associated->bssid;
  791. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  792. printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
  793. sdata->name, bssid, reason_code);
  794. ieee80211_set_disassoc(sdata);
  795. ieee80211_recalc_idle(sdata->local);
  796. return RX_MGMT_CFG80211_DEAUTH;
  797. }
  798. static enum rx_mgmt_action __must_check
  799. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  800. struct ieee80211_mgmt *mgmt, size_t len)
  801. {
  802. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  803. u16 reason_code;
  804. if (len < 24 + 2)
  805. return RX_MGMT_NONE;
  806. ASSERT_MGD_MTX(ifmgd);
  807. if (WARN_ON(!ifmgd->associated))
  808. return RX_MGMT_NONE;
  809. if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
  810. return RX_MGMT_NONE;
  811. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  812. printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
  813. sdata->name, mgmt->sa, reason_code);
  814. ieee80211_set_disassoc(sdata);
  815. ieee80211_recalc_idle(sdata->local);
  816. return RX_MGMT_CFG80211_DISASSOC;
  817. }
  818. static bool ieee80211_assoc_success(struct ieee80211_work *wk,
  819. struct ieee80211_mgmt *mgmt, size_t len)
  820. {
  821. struct ieee80211_sub_if_data *sdata = wk->sdata;
  822. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  823. struct ieee80211_local *local = sdata->local;
  824. struct ieee80211_supported_band *sband;
  825. struct sta_info *sta;
  826. struct cfg80211_bss *cbss = wk->assoc.bss;
  827. u8 *pos;
  828. u32 rates, basic_rates;
  829. u16 capab_info, aid;
  830. struct ieee802_11_elems elems;
  831. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  832. u32 changed = 0;
  833. int i, j, err;
  834. bool have_higher_than_11mbit = false;
  835. u16 ap_ht_cap_flags;
  836. /* AssocResp and ReassocResp have identical structure */
  837. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  838. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  839. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  840. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  841. "set\n", sdata->name, aid);
  842. aid &= ~(BIT(15) | BIT(14));
  843. pos = mgmt->u.assoc_resp.variable;
  844. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  845. if (!elems.supp_rates) {
  846. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  847. sdata->name);
  848. return false;
  849. }
  850. ifmgd->aid = aid;
  851. sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  852. if (!sta) {
  853. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  854. " the AP\n", sdata->name);
  855. return false;
  856. }
  857. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
  858. WLAN_STA_ASSOC_AP);
  859. if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  860. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  861. rates = 0;
  862. basic_rates = 0;
  863. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  864. for (i = 0; i < elems.supp_rates_len; i++) {
  865. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  866. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  867. if (rate > 110)
  868. have_higher_than_11mbit = true;
  869. for (j = 0; j < sband->n_bitrates; j++) {
  870. if (sband->bitrates[j].bitrate == rate) {
  871. rates |= BIT(j);
  872. if (is_basic)
  873. basic_rates |= BIT(j);
  874. break;
  875. }
  876. }
  877. }
  878. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  879. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  880. bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
  881. if (rate > 110)
  882. have_higher_than_11mbit = true;
  883. for (j = 0; j < sband->n_bitrates; j++) {
  884. if (sband->bitrates[j].bitrate == rate) {
  885. rates |= BIT(j);
  886. if (is_basic)
  887. basic_rates |= BIT(j);
  888. break;
  889. }
  890. }
  891. }
  892. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  893. sdata->vif.bss_conf.basic_rates = basic_rates;
  894. /* cf. IEEE 802.11 9.2.12 */
  895. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  896. have_higher_than_11mbit)
  897. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  898. else
  899. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  900. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  901. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  902. elems.ht_cap_elem, &sta->sta.ht_cap);
  903. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  904. rate_control_rate_init(sta);
  905. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  906. set_sta_flags(sta, WLAN_STA_MFP);
  907. if (elems.wmm_param)
  908. set_sta_flags(sta, WLAN_STA_WME);
  909. err = sta_info_insert(sta);
  910. sta = NULL;
  911. if (err) {
  912. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  913. " the AP (error %d)\n", sdata->name, err);
  914. return false;
  915. }
  916. if (elems.wmm_param)
  917. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  918. elems.wmm_param_len);
  919. else
  920. ieee80211_set_wmm_default(sdata);
  921. local->oper_channel = wk->chan;
  922. if (elems.ht_info_elem && elems.wmm_param &&
  923. (sdata->local->hw.queues >= 4) &&
  924. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  925. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  926. cbss->bssid, ap_ht_cap_flags);
  927. /* set AID and assoc capability,
  928. * ieee80211_set_associated() will tell the driver */
  929. bss_conf->aid = aid;
  930. bss_conf->assoc_capability = capab_info;
  931. ieee80211_set_associated(sdata, cbss, changed);
  932. /*
  933. * If we're using 4-addr mode, let the AP know that we're
  934. * doing so, so that it can create the STA VLAN on its side
  935. */
  936. if (ifmgd->use_4addr)
  937. ieee80211_send_4addr_nullfunc(local, sdata);
  938. /*
  939. * Start timer to probe the connection to the AP now.
  940. * Also start the timer that will detect beacon loss.
  941. */
  942. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  943. mod_beacon_timer(sdata);
  944. return true;
  945. }
  946. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  947. struct ieee80211_mgmt *mgmt,
  948. size_t len,
  949. struct ieee80211_rx_status *rx_status,
  950. struct ieee802_11_elems *elems,
  951. bool beacon)
  952. {
  953. struct ieee80211_local *local = sdata->local;
  954. int freq;
  955. struct ieee80211_bss *bss;
  956. struct ieee80211_channel *channel;
  957. bool need_ps = false;
  958. if (sdata->u.mgd.associated) {
  959. bss = (void *)sdata->u.mgd.associated->priv;
  960. /* not previously set so we may need to recalc */
  961. need_ps = !bss->dtim_period;
  962. }
  963. if (elems->ds_params && elems->ds_params_len == 1)
  964. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  965. else
  966. freq = rx_status->freq;
  967. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  968. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  969. return;
  970. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  971. channel, beacon);
  972. if (bss)
  973. ieee80211_rx_bss_put(local, bss);
  974. if (!sdata->u.mgd.associated)
  975. return;
  976. if (need_ps) {
  977. mutex_lock(&local->iflist_mtx);
  978. ieee80211_recalc_ps(local, -1);
  979. mutex_unlock(&local->iflist_mtx);
  980. }
  981. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  982. (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
  983. ETH_ALEN) == 0)) {
  984. struct ieee80211_channel_sw_ie *sw_elem =
  985. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  986. ieee80211_sta_process_chanswitch(sdata, sw_elem, bss);
  987. }
  988. }
  989. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  990. struct sk_buff *skb)
  991. {
  992. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  993. struct ieee80211_if_managed *ifmgd;
  994. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  995. size_t baselen, len = skb->len;
  996. struct ieee802_11_elems elems;
  997. ifmgd = &sdata->u.mgd;
  998. ASSERT_MGD_MTX(ifmgd);
  999. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  1000. return; /* ignore ProbeResp to foreign address */
  1001. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1002. if (baselen > len)
  1003. return;
  1004. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1005. &elems);
  1006. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1007. if (ifmgd->associated &&
  1008. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0 &&
  1009. ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1010. IEEE80211_STA_CONNECTION_POLL)) {
  1011. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1012. IEEE80211_STA_BEACON_POLL);
  1013. mutex_lock(&sdata->local->iflist_mtx);
  1014. ieee80211_recalc_ps(sdata->local, -1);
  1015. mutex_unlock(&sdata->local->iflist_mtx);
  1016. /*
  1017. * We've received a probe response, but are not sure whether
  1018. * we have or will be receiving any beacons or data, so let's
  1019. * schedule the timers again, just in case.
  1020. */
  1021. mod_beacon_timer(sdata);
  1022. mod_timer(&ifmgd->conn_mon_timer,
  1023. round_jiffies_up(jiffies +
  1024. IEEE80211_CONNECTION_IDLE_TIME));
  1025. }
  1026. }
  1027. /*
  1028. * This is the canonical list of information elements we care about,
  1029. * the filter code also gives us all changes to the Microsoft OUI
  1030. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1031. *
  1032. * We implement beacon filtering in software since that means we can
  1033. * avoid processing the frame here and in cfg80211, and userspace
  1034. * will not be able to tell whether the hardware supports it or not.
  1035. *
  1036. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1037. * add items it requires. It also needs to be able to tell us to
  1038. * look out for other vendor IEs.
  1039. */
  1040. static const u64 care_about_ies =
  1041. (1ULL << WLAN_EID_COUNTRY) |
  1042. (1ULL << WLAN_EID_ERP_INFO) |
  1043. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1044. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1045. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1046. (1ULL << WLAN_EID_HT_INFORMATION);
  1047. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1048. struct ieee80211_mgmt *mgmt,
  1049. size_t len,
  1050. struct ieee80211_rx_status *rx_status)
  1051. {
  1052. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1053. size_t baselen;
  1054. struct ieee802_11_elems elems;
  1055. struct ieee80211_local *local = sdata->local;
  1056. u32 changed = 0;
  1057. bool erp_valid, directed_tim = false;
  1058. u8 erp_value = 0;
  1059. u32 ncrc;
  1060. u8 *bssid;
  1061. ASSERT_MGD_MTX(ifmgd);
  1062. /* Process beacon from the current BSS */
  1063. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1064. if (baselen > len)
  1065. return;
  1066. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1067. return;
  1068. /*
  1069. * We might have received a number of frames, among them a
  1070. * disassoc frame and a beacon...
  1071. */
  1072. if (!ifmgd->associated)
  1073. return;
  1074. bssid = ifmgd->associated->bssid;
  1075. /*
  1076. * And in theory even frames from a different AP we were just
  1077. * associated to a split-second ago!
  1078. */
  1079. if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
  1080. return;
  1081. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  1082. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1083. if (net_ratelimit()) {
  1084. printk(KERN_DEBUG "%s: cancelling probereq poll due "
  1085. "to a received beacon\n", sdata->name);
  1086. }
  1087. #endif
  1088. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  1089. mutex_lock(&local->iflist_mtx);
  1090. ieee80211_recalc_ps(local, -1);
  1091. mutex_unlock(&local->iflist_mtx);
  1092. }
  1093. /*
  1094. * Push the beacon loss detection into the future since
  1095. * we are processing a beacon from the AP just now.
  1096. */
  1097. mod_beacon_timer(sdata);
  1098. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  1099. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  1100. len - baselen, &elems,
  1101. care_about_ies, ncrc);
  1102. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1103. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  1104. ifmgd->aid);
  1105. if (ncrc != ifmgd->beacon_crc) {
  1106. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1107. true);
  1108. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1109. elems.wmm_param_len);
  1110. }
  1111. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1112. if (directed_tim) {
  1113. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1114. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1115. ieee80211_hw_config(local,
  1116. IEEE80211_CONF_CHANGE_PS);
  1117. ieee80211_send_nullfunc(local, sdata, 0);
  1118. } else {
  1119. local->pspolling = true;
  1120. /*
  1121. * Here is assumed that the driver will be
  1122. * able to send ps-poll frame and receive a
  1123. * response even though power save mode is
  1124. * enabled, but some drivers might require
  1125. * to disable power save here. This needs
  1126. * to be investigated.
  1127. */
  1128. ieee80211_send_pspoll(local, sdata);
  1129. }
  1130. }
  1131. }
  1132. if (ncrc == ifmgd->beacon_crc)
  1133. return;
  1134. ifmgd->beacon_crc = ncrc;
  1135. if (elems.erp_info && elems.erp_info_len >= 1) {
  1136. erp_valid = true;
  1137. erp_value = elems.erp_info[0];
  1138. } else {
  1139. erp_valid = false;
  1140. }
  1141. changed |= ieee80211_handle_bss_capability(sdata,
  1142. le16_to_cpu(mgmt->u.beacon.capab_info),
  1143. erp_valid, erp_value);
  1144. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1145. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  1146. struct sta_info *sta;
  1147. struct ieee80211_supported_band *sband;
  1148. u16 ap_ht_cap_flags;
  1149. rcu_read_lock();
  1150. sta = sta_info_get(sdata, bssid);
  1151. if (WARN_ON(!sta)) {
  1152. rcu_read_unlock();
  1153. return;
  1154. }
  1155. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1156. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1157. elems.ht_cap_elem, &sta->sta.ht_cap);
  1158. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1159. rcu_read_unlock();
  1160. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1161. bssid, ap_ht_cap_flags);
  1162. }
  1163. /* Note: country IE parsing is done for us by cfg80211 */
  1164. if (elems.country_elem) {
  1165. /* TODO: IBSS also needs this */
  1166. if (elems.pwr_constr_elem)
  1167. ieee80211_handle_pwr_constr(sdata,
  1168. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1169. elems.pwr_constr_elem,
  1170. elems.pwr_constr_elem_len);
  1171. }
  1172. ieee80211_bss_info_change_notify(sdata, changed);
  1173. }
  1174. ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  1175. struct sk_buff *skb)
  1176. {
  1177. struct ieee80211_local *local = sdata->local;
  1178. struct ieee80211_mgmt *mgmt;
  1179. u16 fc;
  1180. if (skb->len < 24)
  1181. return RX_DROP_MONITOR;
  1182. mgmt = (struct ieee80211_mgmt *) skb->data;
  1183. fc = le16_to_cpu(mgmt->frame_control);
  1184. switch (fc & IEEE80211_FCTL_STYPE) {
  1185. case IEEE80211_STYPE_PROBE_RESP:
  1186. case IEEE80211_STYPE_BEACON:
  1187. case IEEE80211_STYPE_DEAUTH:
  1188. case IEEE80211_STYPE_DISASSOC:
  1189. case IEEE80211_STYPE_ACTION:
  1190. skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
  1191. ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
  1192. return RX_QUEUED;
  1193. }
  1194. return RX_DROP_MONITOR;
  1195. }
  1196. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1197. struct sk_buff *skb)
  1198. {
  1199. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1200. struct ieee80211_rx_status *rx_status;
  1201. struct ieee80211_mgmt *mgmt;
  1202. enum rx_mgmt_action rma = RX_MGMT_NONE;
  1203. u16 fc;
  1204. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1205. mgmt = (struct ieee80211_mgmt *) skb->data;
  1206. fc = le16_to_cpu(mgmt->frame_control);
  1207. mutex_lock(&ifmgd->mtx);
  1208. if (ifmgd->associated &&
  1209. memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
  1210. switch (fc & IEEE80211_FCTL_STYPE) {
  1211. case IEEE80211_STYPE_BEACON:
  1212. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1213. rx_status);
  1214. break;
  1215. case IEEE80211_STYPE_PROBE_RESP:
  1216. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  1217. break;
  1218. case IEEE80211_STYPE_DEAUTH:
  1219. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1220. break;
  1221. case IEEE80211_STYPE_DISASSOC:
  1222. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1223. break;
  1224. case IEEE80211_STYPE_ACTION:
  1225. if (mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
  1226. break;
  1227. ieee80211_sta_process_chanswitch(sdata,
  1228. &mgmt->u.action.u.chan_switch.sw_elem,
  1229. (void *)ifmgd->associated->priv);
  1230. break;
  1231. }
  1232. mutex_unlock(&ifmgd->mtx);
  1233. switch (rma) {
  1234. case RX_MGMT_NONE:
  1235. /* no action */
  1236. break;
  1237. case RX_MGMT_CFG80211_DEAUTH:
  1238. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1239. break;
  1240. case RX_MGMT_CFG80211_DISASSOC:
  1241. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  1242. break;
  1243. default:
  1244. WARN(1, "unexpected: %d", rma);
  1245. }
  1246. goto out;
  1247. }
  1248. mutex_unlock(&ifmgd->mtx);
  1249. if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
  1250. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH)
  1251. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1252. out:
  1253. kfree_skb(skb);
  1254. }
  1255. static void ieee80211_sta_timer(unsigned long data)
  1256. {
  1257. struct ieee80211_sub_if_data *sdata =
  1258. (struct ieee80211_sub_if_data *) data;
  1259. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1260. struct ieee80211_local *local = sdata->local;
  1261. if (local->quiescing) {
  1262. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1263. return;
  1264. }
  1265. ieee80211_queue_work(&local->hw, &ifmgd->work);
  1266. }
  1267. static void ieee80211_sta_work(struct work_struct *work)
  1268. {
  1269. struct ieee80211_sub_if_data *sdata =
  1270. container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
  1271. struct ieee80211_local *local = sdata->local;
  1272. struct ieee80211_if_managed *ifmgd;
  1273. struct sk_buff *skb;
  1274. if (!ieee80211_sdata_running(sdata))
  1275. return;
  1276. if (local->scanning)
  1277. return;
  1278. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1279. return;
  1280. /*
  1281. * ieee80211_queue_work() should have picked up most cases,
  1282. * here we'll pick the the rest.
  1283. */
  1284. if (WARN(local->suspended, "STA MLME work scheduled while "
  1285. "going to suspend\n"))
  1286. return;
  1287. ifmgd = &sdata->u.mgd;
  1288. /* first process frames to avoid timing out while a frame is pending */
  1289. while ((skb = skb_dequeue(&ifmgd->skb_queue)))
  1290. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1291. /* then process the rest of the work */
  1292. mutex_lock(&ifmgd->mtx);
  1293. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1294. IEEE80211_STA_CONNECTION_POLL) &&
  1295. ifmgd->associated) {
  1296. u8 bssid[ETH_ALEN];
  1297. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1298. if (time_is_after_jiffies(ifmgd->probe_timeout))
  1299. run_again(ifmgd, ifmgd->probe_timeout);
  1300. else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
  1301. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1302. printk(KERN_DEBUG "No probe response from AP %pM"
  1303. " after %dms, try %d\n", bssid,
  1304. (1000 * IEEE80211_PROBE_WAIT)/HZ,
  1305. ifmgd->probe_send_count);
  1306. #endif
  1307. ieee80211_mgd_probe_ap_send(sdata);
  1308. } else {
  1309. /*
  1310. * We actually lost the connection ... or did we?
  1311. * Let's make sure!
  1312. */
  1313. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1314. IEEE80211_STA_BEACON_POLL);
  1315. printk(KERN_DEBUG "No probe response from AP %pM"
  1316. " after %dms, disconnecting.\n",
  1317. bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
  1318. ieee80211_set_disassoc(sdata);
  1319. ieee80211_recalc_idle(local);
  1320. mutex_unlock(&ifmgd->mtx);
  1321. /*
  1322. * must be outside lock due to cfg80211,
  1323. * but that's not a problem.
  1324. */
  1325. ieee80211_send_deauth_disassoc(sdata, bssid,
  1326. IEEE80211_STYPE_DEAUTH,
  1327. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1328. NULL);
  1329. mutex_lock(&ifmgd->mtx);
  1330. }
  1331. }
  1332. mutex_unlock(&ifmgd->mtx);
  1333. }
  1334. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  1335. {
  1336. struct ieee80211_sub_if_data *sdata =
  1337. (struct ieee80211_sub_if_data *) data;
  1338. struct ieee80211_local *local = sdata->local;
  1339. if (local->quiescing)
  1340. return;
  1341. ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
  1342. }
  1343. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  1344. {
  1345. struct ieee80211_sub_if_data *sdata =
  1346. (struct ieee80211_sub_if_data *) data;
  1347. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1348. struct ieee80211_local *local = sdata->local;
  1349. if (local->quiescing)
  1350. return;
  1351. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  1352. }
  1353. static void ieee80211_sta_monitor_work(struct work_struct *work)
  1354. {
  1355. struct ieee80211_sub_if_data *sdata =
  1356. container_of(work, struct ieee80211_sub_if_data,
  1357. u.mgd.monitor_work);
  1358. ieee80211_mgd_probe_ap(sdata, false);
  1359. }
  1360. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1361. {
  1362. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1363. sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
  1364. IEEE80211_STA_CONNECTION_POLL);
  1365. /* let's probe the connection once */
  1366. ieee80211_queue_work(&sdata->local->hw,
  1367. &sdata->u.mgd.monitor_work);
  1368. /* and do all the other regular work too */
  1369. ieee80211_queue_work(&sdata->local->hw,
  1370. &sdata->u.mgd.work);
  1371. }
  1372. }
  1373. #ifdef CONFIG_PM
  1374. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  1375. {
  1376. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1377. /*
  1378. * we need to use atomic bitops for the running bits
  1379. * only because both timers might fire at the same
  1380. * time -- the code here is properly synchronised.
  1381. */
  1382. cancel_work_sync(&ifmgd->work);
  1383. cancel_work_sync(&ifmgd->beacon_loss_work);
  1384. if (del_timer_sync(&ifmgd->timer))
  1385. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1386. cancel_work_sync(&ifmgd->chswitch_work);
  1387. if (del_timer_sync(&ifmgd->chswitch_timer))
  1388. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  1389. cancel_work_sync(&ifmgd->monitor_work);
  1390. /* these will just be re-established on connection */
  1391. del_timer_sync(&ifmgd->conn_mon_timer);
  1392. del_timer_sync(&ifmgd->bcn_mon_timer);
  1393. }
  1394. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  1395. {
  1396. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1397. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  1398. add_timer(&ifmgd->timer);
  1399. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  1400. add_timer(&ifmgd->chswitch_timer);
  1401. }
  1402. #endif
  1403. /* interface setup */
  1404. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1405. {
  1406. struct ieee80211_if_managed *ifmgd;
  1407. ifmgd = &sdata->u.mgd;
  1408. INIT_WORK(&ifmgd->work, ieee80211_sta_work);
  1409. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  1410. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1411. INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
  1412. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1413. (unsigned long) sdata);
  1414. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  1415. (unsigned long) sdata);
  1416. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  1417. (unsigned long) sdata);
  1418. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1419. (unsigned long) sdata);
  1420. skb_queue_head_init(&ifmgd->skb_queue);
  1421. ifmgd->flags = 0;
  1422. mutex_init(&ifmgd->mtx);
  1423. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  1424. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  1425. else
  1426. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  1427. }
  1428. /* scan finished notification */
  1429. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1430. {
  1431. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1432. /* Restart STA timers */
  1433. rcu_read_lock();
  1434. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1435. ieee80211_restart_sta_timer(sdata);
  1436. rcu_read_unlock();
  1437. }
  1438. int ieee80211_max_network_latency(struct notifier_block *nb,
  1439. unsigned long data, void *dummy)
  1440. {
  1441. s32 latency_usec = (s32) data;
  1442. struct ieee80211_local *local =
  1443. container_of(nb, struct ieee80211_local,
  1444. network_latency_notifier);
  1445. mutex_lock(&local->iflist_mtx);
  1446. ieee80211_recalc_ps(local, latency_usec);
  1447. mutex_unlock(&local->iflist_mtx);
  1448. return 0;
  1449. }
  1450. /* config hooks */
  1451. static enum work_done_result
  1452. ieee80211_probe_auth_done(struct ieee80211_work *wk,
  1453. struct sk_buff *skb)
  1454. {
  1455. if (!skb) {
  1456. cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
  1457. return WORK_DONE_DESTROY;
  1458. }
  1459. if (wk->type == IEEE80211_WORK_AUTH) {
  1460. cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
  1461. return WORK_DONE_DESTROY;
  1462. }
  1463. mutex_lock(&wk->sdata->u.mgd.mtx);
  1464. ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
  1465. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1466. wk->type = IEEE80211_WORK_AUTH;
  1467. wk->probe_auth.tries = 0;
  1468. return WORK_DONE_REQUEUE;
  1469. }
  1470. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  1471. struct cfg80211_auth_request *req)
  1472. {
  1473. const u8 *ssid;
  1474. struct ieee80211_work *wk;
  1475. u16 auth_alg;
  1476. switch (req->auth_type) {
  1477. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  1478. auth_alg = WLAN_AUTH_OPEN;
  1479. break;
  1480. case NL80211_AUTHTYPE_SHARED_KEY:
  1481. auth_alg = WLAN_AUTH_SHARED_KEY;
  1482. break;
  1483. case NL80211_AUTHTYPE_FT:
  1484. auth_alg = WLAN_AUTH_FT;
  1485. break;
  1486. case NL80211_AUTHTYPE_NETWORK_EAP:
  1487. auth_alg = WLAN_AUTH_LEAP;
  1488. break;
  1489. default:
  1490. return -EOPNOTSUPP;
  1491. }
  1492. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1493. if (!wk)
  1494. return -ENOMEM;
  1495. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
  1496. if (req->ie && req->ie_len) {
  1497. memcpy(wk->ie, req->ie, req->ie_len);
  1498. wk->ie_len = req->ie_len;
  1499. }
  1500. if (req->key && req->key_len) {
  1501. wk->probe_auth.key_len = req->key_len;
  1502. wk->probe_auth.key_idx = req->key_idx;
  1503. memcpy(wk->probe_auth.key, req->key, req->key_len);
  1504. }
  1505. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  1506. memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
  1507. wk->probe_auth.ssid_len = ssid[1];
  1508. wk->probe_auth.algorithm = auth_alg;
  1509. wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
  1510. /* if we already have a probe, don't probe again */
  1511. if (req->bss->proberesp_ies)
  1512. wk->type = IEEE80211_WORK_AUTH;
  1513. else
  1514. wk->type = IEEE80211_WORK_DIRECT_PROBE;
  1515. wk->chan = req->bss->channel;
  1516. wk->sdata = sdata;
  1517. wk->done = ieee80211_probe_auth_done;
  1518. ieee80211_add_work(wk);
  1519. return 0;
  1520. }
  1521. static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
  1522. struct sk_buff *skb)
  1523. {
  1524. struct ieee80211_mgmt *mgmt;
  1525. u16 status;
  1526. if (!skb) {
  1527. cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
  1528. return WORK_DONE_DESTROY;
  1529. }
  1530. mgmt = (void *)skb->data;
  1531. status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1532. if (status == WLAN_STATUS_SUCCESS) {
  1533. mutex_lock(&wk->sdata->u.mgd.mtx);
  1534. if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
  1535. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1536. /* oops -- internal error -- send timeout for now */
  1537. cfg80211_send_assoc_timeout(wk->sdata->dev,
  1538. wk->filter_ta);
  1539. return WORK_DONE_DESTROY;
  1540. }
  1541. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1542. }
  1543. cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
  1544. return WORK_DONE_DESTROY;
  1545. }
  1546. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  1547. struct cfg80211_assoc_request *req)
  1548. {
  1549. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1550. struct ieee80211_bss *bss = (void *)req->bss->priv;
  1551. struct ieee80211_work *wk;
  1552. const u8 *ssid;
  1553. int i;
  1554. mutex_lock(&ifmgd->mtx);
  1555. if (ifmgd->associated) {
  1556. if (!req->prev_bssid ||
  1557. memcmp(req->prev_bssid, ifmgd->associated->bssid,
  1558. ETH_ALEN)) {
  1559. /*
  1560. * We are already associated and the request was not a
  1561. * reassociation request from the current BSS, so
  1562. * reject it.
  1563. */
  1564. mutex_unlock(&ifmgd->mtx);
  1565. return -EALREADY;
  1566. }
  1567. /* Trying to reassociate - clear previous association state */
  1568. ieee80211_set_disassoc(sdata);
  1569. }
  1570. mutex_unlock(&ifmgd->mtx);
  1571. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1572. if (!wk)
  1573. return -ENOMEM;
  1574. ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
  1575. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1576. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
  1577. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  1578. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  1579. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
  1580. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  1581. if (req->ie && req->ie_len) {
  1582. memcpy(wk->ie, req->ie, req->ie_len);
  1583. wk->ie_len = req->ie_len;
  1584. } else
  1585. wk->ie_len = 0;
  1586. wk->assoc.bss = req->bss;
  1587. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
  1588. /* new association always uses requested smps mode */
  1589. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  1590. if (ifmgd->powersave)
  1591. ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
  1592. else
  1593. ifmgd->ap_smps = IEEE80211_SMPS_OFF;
  1594. } else
  1595. ifmgd->ap_smps = ifmgd->req_smps;
  1596. wk->assoc.smps = ifmgd->ap_smps;
  1597. /*
  1598. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  1599. * We still associate in non-HT mode (11a/b/g) if any one of these
  1600. * ciphers is configured as pairwise.
  1601. * We can set this to true for non-11n hardware, that'll be checked
  1602. * separately along with the peer capabilities.
  1603. */
  1604. wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
  1605. wk->assoc.capability = req->bss->capability;
  1606. wk->assoc.wmm_used = bss->wmm_used;
  1607. wk->assoc.supp_rates = bss->supp_rates;
  1608. wk->assoc.supp_rates_len = bss->supp_rates_len;
  1609. wk->assoc.ht_information_ie =
  1610. ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
  1611. if (bss->wmm_used && bss->uapsd_supported &&
  1612. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  1613. wk->assoc.uapsd_used = true;
  1614. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  1615. } else {
  1616. wk->assoc.uapsd_used = false;
  1617. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  1618. }
  1619. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  1620. memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
  1621. wk->assoc.ssid_len = ssid[1];
  1622. if (req->prev_bssid)
  1623. memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
  1624. wk->type = IEEE80211_WORK_ASSOC;
  1625. wk->chan = req->bss->channel;
  1626. wk->sdata = sdata;
  1627. wk->done = ieee80211_assoc_done;
  1628. if (req->use_mfp) {
  1629. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  1630. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  1631. } else {
  1632. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  1633. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  1634. }
  1635. if (req->crypto.control_port)
  1636. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  1637. else
  1638. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  1639. ieee80211_add_work(wk);
  1640. return 0;
  1641. }
  1642. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  1643. struct cfg80211_deauth_request *req,
  1644. void *cookie)
  1645. {
  1646. struct ieee80211_local *local = sdata->local;
  1647. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1648. struct ieee80211_work *wk;
  1649. const u8 *bssid = req->bss->bssid;
  1650. mutex_lock(&ifmgd->mtx);
  1651. if (ifmgd->associated == req->bss) {
  1652. bssid = req->bss->bssid;
  1653. ieee80211_set_disassoc(sdata);
  1654. mutex_unlock(&ifmgd->mtx);
  1655. } else {
  1656. bool not_auth_yet = false;
  1657. mutex_unlock(&ifmgd->mtx);
  1658. mutex_lock(&local->work_mtx);
  1659. list_for_each_entry(wk, &local->work_list, list) {
  1660. if (wk->sdata != sdata)
  1661. continue;
  1662. if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
  1663. wk->type != IEEE80211_WORK_AUTH &&
  1664. wk->type != IEEE80211_WORK_ASSOC)
  1665. continue;
  1666. if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
  1667. continue;
  1668. not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
  1669. list_del_rcu(&wk->list);
  1670. free_work(wk);
  1671. break;
  1672. }
  1673. mutex_unlock(&local->work_mtx);
  1674. /*
  1675. * If somebody requests authentication and we haven't
  1676. * sent out an auth frame yet there's no need to send
  1677. * out a deauth frame either. If the state was PROBE,
  1678. * then this is the case. If it's AUTH we have sent a
  1679. * frame, and if it's IDLE we have completed the auth
  1680. * process already.
  1681. */
  1682. if (not_auth_yet) {
  1683. __cfg80211_auth_canceled(sdata->dev, bssid);
  1684. return 0;
  1685. }
  1686. }
  1687. printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
  1688. sdata->name, bssid, req->reason_code);
  1689. ieee80211_send_deauth_disassoc(sdata, bssid,
  1690. IEEE80211_STYPE_DEAUTH, req->reason_code,
  1691. cookie);
  1692. ieee80211_recalc_idle(sdata->local);
  1693. return 0;
  1694. }
  1695. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  1696. struct cfg80211_disassoc_request *req,
  1697. void *cookie)
  1698. {
  1699. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1700. mutex_lock(&ifmgd->mtx);
  1701. /*
  1702. * cfg80211 should catch this ... but it's racy since
  1703. * we can receive a disassoc frame, process it, hand it
  1704. * to cfg80211 while that's in a locked section already
  1705. * trying to tell us that the user wants to disconnect.
  1706. */
  1707. if (ifmgd->associated != req->bss) {
  1708. mutex_unlock(&ifmgd->mtx);
  1709. return -ENOLINK;
  1710. }
  1711. printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
  1712. sdata->name, req->bss->bssid, req->reason_code);
  1713. ieee80211_set_disassoc(sdata);
  1714. mutex_unlock(&ifmgd->mtx);
  1715. ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
  1716. IEEE80211_STYPE_DISASSOC, req->reason_code,
  1717. cookie);
  1718. ieee80211_recalc_idle(sdata->local);
  1719. return 0;
  1720. }
  1721. int ieee80211_mgd_action(struct ieee80211_sub_if_data *sdata,
  1722. struct ieee80211_channel *chan,
  1723. enum nl80211_channel_type channel_type,
  1724. const u8 *buf, size_t len, u64 *cookie)
  1725. {
  1726. struct ieee80211_local *local = sdata->local;
  1727. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1728. struct sk_buff *skb;
  1729. /* Check that we are on the requested channel for transmission */
  1730. if ((chan != local->tmp_channel ||
  1731. channel_type != local->tmp_channel_type) &&
  1732. (chan != local->oper_channel ||
  1733. channel_type != local->oper_channel_type))
  1734. return -EBUSY;
  1735. skb = dev_alloc_skb(local->hw.extra_tx_headroom + len);
  1736. if (!skb)
  1737. return -ENOMEM;
  1738. skb_reserve(skb, local->hw.extra_tx_headroom);
  1739. memcpy(skb_put(skb, len), buf, len);
  1740. if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
  1741. IEEE80211_SKB_CB(skb)->flags |=
  1742. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1743. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX |
  1744. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1745. skb->dev = sdata->dev;
  1746. ieee80211_tx_skb(sdata, skb);
  1747. *cookie = (unsigned long) skb;
  1748. return 0;
  1749. }