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 
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/pm_qos_params.h>
21 #include <linux/crc32.h>
22 #include <linux/slab.h>
23 #include <net/mac80211.h>
24 #include <asm/unaligned.h>
25 
26 #include "ieee80211_i.h"
27 #include "driver-ops.h"
28 #include "rate.h"
29 #include "led.h"
30 
31 static int max_nullfunc_tries = 2;
32 module_param(max_nullfunc_tries, int, 0644);
33 MODULE_PARM_DESC(max_nullfunc_tries,
34 		 "Maximum nullfunc tx tries before disconnecting (reason 4).");
35 
36 static int max_probe_tries = 5;
37 module_param(max_probe_tries, int, 0644);
38 MODULE_PARM_DESC(max_probe_tries,
39 		 "Maximum probe tries before disconnecting (reason 4).");
40 
41 /*
42  * Beacon loss timeout is calculated as N frames times the
43  * advertised beacon interval.  This may need to be somewhat
44  * higher than what hardware might detect to account for
45  * delays in the host processing frames. But since we also
46  * probe on beacon miss before declaring the connection lost
47  * default to what we want.
48  */
49 #define IEEE80211_BEACON_LOSS_COUNT	7
50 
51 /*
52  * Time the connection can be idle before we probe
53  * it to see if we can still talk to the AP.
54  */
55 #define IEEE80211_CONNECTION_IDLE_TIME	(30 * HZ)
56 /*
57  * Time we wait for a probe response after sending
58  * a probe request because of beacon loss or for
59  * checking the connection still works.
60  */
61 static int probe_wait_ms = 500;
62 module_param(probe_wait_ms, int, 0644);
63 MODULE_PARM_DESC(probe_wait_ms,
64 		 "Maximum time(ms) to wait for probe response"
65 		 " before disconnecting (reason 4).");
66 
67 /*
68  * Weight given to the latest Beacon frame when calculating average signal
69  * strength for Beacon frames received in the current BSS. This must be
70  * between 1 and 15.
71  */
72 #define IEEE80211_SIGNAL_AVE_WEIGHT	3
73 
74 /*
75  * How many Beacon frames need to have been used in average signal strength
76  * before starting to indicate signal change events.
77  */
78 #define IEEE80211_SIGNAL_AVE_MIN_COUNT	4
79 
80 #define TMR_RUNNING_TIMER	0
81 #define TMR_RUNNING_CHANSW	1
82 
83 /*
84  * All cfg80211 functions have to be called outside a locked
85  * section so that they can acquire a lock themselves... This
86  * is much simpler than queuing up things in cfg80211, but we
87  * do need some indirection for that here.
88  */
89 enum rx_mgmt_action {
90 	/* no action required */
91 	RX_MGMT_NONE,
92 
93 	/* caller must call cfg80211_send_rx_auth() */
94 	RX_MGMT_CFG80211_AUTH,
95 
96 	/* caller must call cfg80211_send_rx_assoc() */
97 	RX_MGMT_CFG80211_ASSOC,
98 
99 	/* caller must call cfg80211_send_deauth() */
100 	RX_MGMT_CFG80211_DEAUTH,
101 
102 	/* caller must call cfg80211_send_disassoc() */
103 	RX_MGMT_CFG80211_DISASSOC,
104 
105 	/* caller must tell cfg80211 about internal error */
106 	RX_MGMT_CFG80211_ASSOC_ERROR,
107 };
108 
109 /* utils */
ASSERT_MGD_MTX(struct ieee80211_if_managed * ifmgd)110 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
111 {
112 	lockdep_assert_held(&ifmgd->mtx);
113 }
114 
115 /*
116  * We can have multiple work items (and connection probing)
117  * scheduling this timer, but we need to take care to only
118  * reschedule it when it should fire _earlier_ than it was
119  * asked for before, or if it's not pending right now. This
120  * function ensures that. Note that it then is required to
121  * run this function for all timeouts after the first one
122  * has happened -- the work that runs from this timer will
123  * do that.
124  */
run_again(struct ieee80211_if_managed * ifmgd,unsigned long timeout)125 static void run_again(struct ieee80211_if_managed *ifmgd,
126 			     unsigned long timeout)
127 {
128 	ASSERT_MGD_MTX(ifmgd);
129 
130 	if (!timer_pending(&ifmgd->timer) ||
131 	    time_before(timeout, ifmgd->timer.expires))
132 		mod_timer(&ifmgd->timer, timeout);
133 }
134 
ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data * sdata)135 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
136 {
137 	if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
138 		return;
139 
140 	mod_timer(&sdata->u.mgd.bcn_mon_timer,
141 		  round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
142 }
143 
ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data * sdata)144 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
145 {
146 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
147 
148 	if (unlikely(!sdata->u.mgd.associated))
149 		return;
150 
151 	if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
152 		return;
153 
154 	mod_timer(&sdata->u.mgd.conn_mon_timer,
155 		  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
156 
157 	ifmgd->probe_send_count = 0;
158 }
159 
ecw2cw(int ecw)160 static int ecw2cw(int ecw)
161 {
162 	return (1 << ecw) - 1;
163 }
164 
165 /*
166  * ieee80211_enable_ht should be called only after the operating band
167  * has been determined as ht configuration depends on the hw's
168  * HT abilities for a specific band.
169  */
ieee80211_enable_ht(struct ieee80211_sub_if_data * sdata,struct ieee80211_ht_info * hti,const u8 * bssid,u16 ap_ht_cap_flags)170 static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
171 			       struct ieee80211_ht_info *hti,
172 			       const u8 *bssid, u16 ap_ht_cap_flags)
173 {
174 	struct ieee80211_local *local = sdata->local;
175 	struct ieee80211_supported_band *sband;
176 	struct sta_info *sta;
177 	u32 changed = 0;
178 	int hti_cfreq;
179 	u16 ht_opmode;
180 	bool enable_ht = true;
181 	enum nl80211_channel_type prev_chantype;
182 	enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
183 
184 	sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
185 
186 	prev_chantype = sdata->vif.bss_conf.channel_type;
187 
188 	/* HT is not supported */
189 	if (!sband->ht_cap.ht_supported)
190 		enable_ht = false;
191 
192 	if (enable_ht) {
193 		hti_cfreq = ieee80211_channel_to_frequency(hti->control_chan,
194 							   sband->band);
195 		/* check that channel matches the right operating channel */
196 		if (local->hw.conf.channel->center_freq != hti_cfreq) {
197 			/* Some APs mess this up, evidently.
198 			 * Netgear WNDR3700 sometimes reports 4 higher than
199 			 * the actual channel, for instance.
200 			 */
201 			printk(KERN_DEBUG
202 			       "%s: Wrong control channel in association"
203 			       " response: configured center-freq: %d"
204 			       " hti-cfreq: %d  hti->control_chan: %d"
205 			       " band: %d.  Disabling HT.\n",
206 			       sdata->name,
207 			       local->hw.conf.channel->center_freq,
208 			       hti_cfreq, hti->control_chan,
209 			       sband->band);
210 			enable_ht = false;
211 		}
212 	}
213 
214 	if (enable_ht) {
215 		channel_type = NL80211_CHAN_HT20;
216 
217 		if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
218 		    (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
219 		    (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
220 			switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
221 			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
222 				if (!(local->hw.conf.channel->flags &
223 				    IEEE80211_CHAN_NO_HT40PLUS))
224 					channel_type = NL80211_CHAN_HT40PLUS;
225 				break;
226 			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
227 				if (!(local->hw.conf.channel->flags &
228 				    IEEE80211_CHAN_NO_HT40MINUS))
229 					channel_type = NL80211_CHAN_HT40MINUS;
230 				break;
231 			}
232 		}
233 	}
234 
235 	if (local->tmp_channel)
236 		local->tmp_channel_type = channel_type;
237 
238 	if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
239 		/* can only fail due to HT40+/- mismatch */
240 		channel_type = NL80211_CHAN_HT20;
241 		WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
242 	}
243 
244 	/* channel_type change automatically detected */
245 	ieee80211_hw_config(local, 0);
246 
247 	if (prev_chantype != channel_type) {
248 		rcu_read_lock();
249 		sta = sta_info_get(sdata, bssid);
250 		if (sta)
251 			rate_control_rate_update(local, sband, sta,
252 						 IEEE80211_RC_HT_CHANGED,
253 						 channel_type);
254 		rcu_read_unlock();
255 	}
256 
257 	ht_opmode = le16_to_cpu(hti->operation_mode);
258 
259 	/* if bss configuration changed store the new one */
260 	if (sdata->ht_opmode_valid != enable_ht ||
261 	    sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
262 	    prev_chantype != channel_type) {
263 		changed |= BSS_CHANGED_HT;
264 		sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
265 		sdata->ht_opmode_valid = enable_ht;
266 	}
267 
268 	return changed;
269 }
270 
271 /* frame sending functions */
272 
ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data * sdata,const u8 * bssid,u16 stype,u16 reason,void * cookie,bool send_frame)273 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
274 					   const u8 *bssid, u16 stype, u16 reason,
275 					   void *cookie, bool send_frame)
276 {
277 	struct ieee80211_local *local = sdata->local;
278 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
279 	struct sk_buff *skb;
280 	struct ieee80211_mgmt *mgmt;
281 
282 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
283 	if (!skb) {
284 		printk(KERN_DEBUG "%s: failed to allocate buffer for "
285 		       "deauth/disassoc frame\n", sdata->name);
286 		return;
287 	}
288 	skb_reserve(skb, local->hw.extra_tx_headroom);
289 
290 	mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
291 	memset(mgmt, 0, 24);
292 	memcpy(mgmt->da, bssid, ETH_ALEN);
293 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
294 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
295 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
296 	skb_put(skb, 2);
297 	/* u.deauth.reason_code == u.disassoc.reason_code */
298 	mgmt->u.deauth.reason_code = cpu_to_le16(reason);
299 
300 	if (stype == IEEE80211_STYPE_DEAUTH)
301 		if (cookie)
302 			__cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
303 		else
304 			cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
305 	else
306 		if (cookie)
307 			__cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
308 		else
309 			cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
310 	if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
311 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
312 
313 	if (send_frame)
314 		ieee80211_tx_skb(sdata, skb);
315 	else
316 		kfree_skb(skb);
317 }
318 
ieee80211_send_pspoll(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata)319 void ieee80211_send_pspoll(struct ieee80211_local *local,
320 			   struct ieee80211_sub_if_data *sdata)
321 {
322 	struct ieee80211_pspoll *pspoll;
323 	struct sk_buff *skb;
324 
325 	skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
326 	if (!skb)
327 		return;
328 
329 	pspoll = (struct ieee80211_pspoll *) skb->data;
330 	pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
331 
332 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
333 	ieee80211_tx_skb(sdata, skb);
334 }
335 
ieee80211_send_nullfunc(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,int powersave)336 void ieee80211_send_nullfunc(struct ieee80211_local *local,
337 			     struct ieee80211_sub_if_data *sdata,
338 			     int powersave)
339 {
340 	struct sk_buff *skb;
341 	struct ieee80211_hdr_3addr *nullfunc;
342 
343 	skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
344 	if (!skb)
345 		return;
346 
347 	nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
348 	if (powersave)
349 		nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
350 
351 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
352 	ieee80211_tx_skb(sdata, skb);
353 }
354 
ieee80211_send_4addr_nullfunc(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata)355 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
356 					  struct ieee80211_sub_if_data *sdata)
357 {
358 	struct sk_buff *skb;
359 	struct ieee80211_hdr *nullfunc;
360 	__le16 fc;
361 
362 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
363 		return;
364 
365 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
366 	if (!skb) {
367 		printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
368 		       "nullfunc frame\n", sdata->name);
369 		return;
370 	}
371 	skb_reserve(skb, local->hw.extra_tx_headroom);
372 
373 	nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
374 	memset(nullfunc, 0, 30);
375 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
376 			 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
377 	nullfunc->frame_control = fc;
378 	memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
379 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
380 	memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
381 	memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
382 
383 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
384 	ieee80211_tx_skb(sdata, skb);
385 }
386 
387 /* spectrum management related things */
ieee80211_chswitch_work(struct work_struct * work)388 static void ieee80211_chswitch_work(struct work_struct *work)
389 {
390 	struct ieee80211_sub_if_data *sdata =
391 		container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
392 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
393 
394 	if (!ieee80211_sdata_running(sdata))
395 		return;
396 
397 	mutex_lock(&ifmgd->mtx);
398 	if (!ifmgd->associated)
399 		goto out;
400 
401 	sdata->local->oper_channel = sdata->local->csa_channel;
402 	if (!sdata->local->ops->channel_switch) {
403 		/* call "hw_config" only if doing sw channel switch */
404 		ieee80211_hw_config(sdata->local,
405 			IEEE80211_CONF_CHANGE_CHANNEL);
406 	}
407 
408 	/* XXX: shouldn't really modify cfg80211-owned data! */
409 	ifmgd->associated->channel = sdata->local->oper_channel;
410 
411 	ieee80211_wake_queues_by_reason(&sdata->local->hw,
412 					IEEE80211_QUEUE_STOP_REASON_CSA);
413  out:
414 	ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
415 	mutex_unlock(&ifmgd->mtx);
416 }
417 
ieee80211_chswitch_done(struct ieee80211_vif * vif,bool success)418 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
419 {
420 	struct ieee80211_sub_if_data *sdata;
421 	struct ieee80211_if_managed *ifmgd;
422 
423 	sdata = vif_to_sdata(vif);
424 	ifmgd = &sdata->u.mgd;
425 
426 	trace_api_chswitch_done(sdata, success);
427 	if (!success) {
428 		/*
429 		 * If the channel switch was not successful, stay
430 		 * around on the old channel. We currently lack
431 		 * good handling of this situation, possibly we
432 		 * should just drop the association.
433 		 */
434 		sdata->local->csa_channel = sdata->local->oper_channel;
435 	}
436 
437 	ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
438 }
439 EXPORT_SYMBOL(ieee80211_chswitch_done);
440 
ieee80211_chswitch_timer(unsigned long data)441 static void ieee80211_chswitch_timer(unsigned long data)
442 {
443 	struct ieee80211_sub_if_data *sdata =
444 		(struct ieee80211_sub_if_data *) data;
445 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
446 
447 	if (sdata->local->quiescing) {
448 		set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
449 		return;
450 	}
451 
452 	ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
453 }
454 
ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data * sdata,struct ieee80211_channel_sw_ie * sw_elem,struct ieee80211_bss * bss,u64 timestamp)455 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
456 				      struct ieee80211_channel_sw_ie *sw_elem,
457 				      struct ieee80211_bss *bss,
458 				      u64 timestamp)
459 {
460 	struct cfg80211_bss *cbss =
461 		container_of((void *)bss, struct cfg80211_bss, priv);
462 	struct ieee80211_channel *new_ch;
463 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
464 	int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
465 						      cbss->channel->band);
466 
467 	ASSERT_MGD_MTX(ifmgd);
468 
469 	if (!ifmgd->associated)
470 		return;
471 
472 	if (sdata->local->scanning)
473 		return;
474 
475 	/* Disregard subsequent beacons if we are already running a timer
476 	   processing a CSA */
477 
478 	if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
479 		return;
480 
481 	new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
482 	if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
483 		return;
484 
485 	sdata->local->csa_channel = new_ch;
486 
487 	if (sdata->local->ops->channel_switch) {
488 		/* use driver's channel switch callback */
489 		struct ieee80211_channel_switch ch_switch;
490 		memset(&ch_switch, 0, sizeof(ch_switch));
491 		ch_switch.timestamp = timestamp;
492 		if (sw_elem->mode) {
493 			ch_switch.block_tx = true;
494 			ieee80211_stop_queues_by_reason(&sdata->local->hw,
495 					IEEE80211_QUEUE_STOP_REASON_CSA);
496 		}
497 		ch_switch.channel = new_ch;
498 		ch_switch.count = sw_elem->count;
499 		ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
500 		drv_channel_switch(sdata->local, &ch_switch);
501 		return;
502 	}
503 
504 	/* channel switch handled in software */
505 	if (sw_elem->count <= 1) {
506 		ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
507 	} else {
508 		if (sw_elem->mode)
509 			ieee80211_stop_queues_by_reason(&sdata->local->hw,
510 					IEEE80211_QUEUE_STOP_REASON_CSA);
511 		ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
512 		mod_timer(&ifmgd->chswitch_timer,
513 			  jiffies +
514 			  msecs_to_jiffies(sw_elem->count *
515 					   cbss->beacon_interval));
516 	}
517 }
518 
ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data * sdata,u16 capab_info,u8 * pwr_constr_elem,u8 pwr_constr_elem_len)519 static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
520 					u16 capab_info, u8 *pwr_constr_elem,
521 					u8 pwr_constr_elem_len)
522 {
523 	struct ieee80211_conf *conf = &sdata->local->hw.conf;
524 
525 	if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
526 		return;
527 
528 	/* Power constraint IE length should be 1 octet */
529 	if (pwr_constr_elem_len != 1)
530 		return;
531 
532 	if ((*pwr_constr_elem <= conf->channel->max_power) &&
533 	    (*pwr_constr_elem != sdata->local->power_constr_level)) {
534 		sdata->local->power_constr_level = *pwr_constr_elem;
535 		ieee80211_hw_config(sdata->local, 0);
536 	}
537 }
538 
ieee80211_enable_dyn_ps(struct ieee80211_vif * vif)539 void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
540 {
541 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
542 	struct ieee80211_local *local = sdata->local;
543 	struct ieee80211_conf *conf = &local->hw.conf;
544 
545 	WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
546 		!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
547 		(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
548 
549 	local->disable_dynamic_ps = false;
550 	conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
551 }
552 EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
553 
ieee80211_disable_dyn_ps(struct ieee80211_vif * vif)554 void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
555 {
556 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
557 	struct ieee80211_local *local = sdata->local;
558 	struct ieee80211_conf *conf = &local->hw.conf;
559 
560 	WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
561 		!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
562 		(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
563 
564 	local->disable_dynamic_ps = true;
565 	conf->dynamic_ps_timeout = 0;
566 	del_timer_sync(&local->dynamic_ps_timer);
567 	ieee80211_queue_work(&local->hw,
568 			     &local->dynamic_ps_enable_work);
569 }
570 EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
571 
572 /* powersave */
ieee80211_enable_ps(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata)573 static void ieee80211_enable_ps(struct ieee80211_local *local,
574 				struct ieee80211_sub_if_data *sdata)
575 {
576 	struct ieee80211_conf *conf = &local->hw.conf;
577 
578 	/*
579 	 * If we are scanning right now then the parameters will
580 	 * take effect when scan finishes.
581 	 */
582 	if (local->scanning)
583 		return;
584 
585 	if (conf->dynamic_ps_timeout > 0 &&
586 	    !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
587 		mod_timer(&local->dynamic_ps_timer, jiffies +
588 			  msecs_to_jiffies(conf->dynamic_ps_timeout));
589 	} else {
590 		if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
591 			ieee80211_send_nullfunc(local, sdata, 1);
592 
593 		if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
594 		    (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
595 			return;
596 
597 		conf->flags |= IEEE80211_CONF_PS;
598 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
599 	}
600 }
601 
ieee80211_change_ps(struct ieee80211_local * local)602 static void ieee80211_change_ps(struct ieee80211_local *local)
603 {
604 	struct ieee80211_conf *conf = &local->hw.conf;
605 
606 	if (local->ps_sdata) {
607 		ieee80211_enable_ps(local, local->ps_sdata);
608 	} else if (conf->flags & IEEE80211_CONF_PS) {
609 		conf->flags &= ~IEEE80211_CONF_PS;
610 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
611 		del_timer_sync(&local->dynamic_ps_timer);
612 		cancel_work_sync(&local->dynamic_ps_enable_work);
613 	}
614 }
615 
ieee80211_powersave_allowed(struct ieee80211_sub_if_data * sdata)616 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
617 {
618 	struct ieee80211_if_managed *mgd = &sdata->u.mgd;
619 	struct sta_info *sta = NULL;
620 	u32 sta_flags = 0;
621 
622 	if (!mgd->powersave)
623 		return false;
624 
625 	if (!mgd->associated)
626 		return false;
627 
628 	if (!mgd->associated->beacon_ies)
629 		return false;
630 
631 	if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
632 			  IEEE80211_STA_CONNECTION_POLL))
633 		return false;
634 
635 	rcu_read_lock();
636 	sta = sta_info_get(sdata, mgd->bssid);
637 	if (sta)
638 		sta_flags = get_sta_flags(sta);
639 	rcu_read_unlock();
640 
641 	if (!(sta_flags & WLAN_STA_AUTHORIZED))
642 		return false;
643 
644 	return true;
645 }
646 
647 /* need to hold RTNL or interface lock */
ieee80211_recalc_ps(struct ieee80211_local * local,s32 latency)648 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
649 {
650 	struct ieee80211_sub_if_data *sdata, *found = NULL;
651 	int count = 0;
652 	int timeout;
653 
654 	if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
655 		local->ps_sdata = NULL;
656 		return;
657 	}
658 
659 	if (!list_empty(&local->work_list)) {
660 		local->ps_sdata = NULL;
661 		goto change;
662 	}
663 
664 	list_for_each_entry(sdata, &local->interfaces, list) {
665 		if (!ieee80211_sdata_running(sdata))
666 			continue;
667 		if (sdata->vif.type == NL80211_IFTYPE_AP) {
668 			/* If an AP vif is found, then disable PS
669 			 * by setting the count to zero thereby setting
670 			 * ps_sdata to NULL.
671 			 */
672 			count = 0;
673 			break;
674 		}
675 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
676 			continue;
677 		found = sdata;
678 		count++;
679 	}
680 
681 	if (count == 1 && ieee80211_powersave_allowed(found)) {
682 		struct ieee80211_conf *conf = &local->hw.conf;
683 		s32 beaconint_us;
684 
685 		if (latency < 0)
686 			latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
687 
688 		beaconint_us = ieee80211_tu_to_usec(
689 					found->vif.bss_conf.beacon_int);
690 
691 		timeout = local->dynamic_ps_forced_timeout;
692 		if (timeout < 0) {
693 			/*
694 			 * Go to full PSM if the user configures a very low
695 			 * latency requirement.
696 			 * The 2000 second value is there for compatibility
697 			 * until the PM_QOS_NETWORK_LATENCY is configured
698 			 * with real values.
699 			 */
700 			if (latency > (1900 * USEC_PER_MSEC) &&
701 			    latency != (2000 * USEC_PER_SEC))
702 				timeout = 0;
703 			else
704 				timeout = 100;
705 		}
706 		local->dynamic_ps_user_timeout = timeout;
707 		if (!local->disable_dynamic_ps)
708 			conf->dynamic_ps_timeout =
709 				local->dynamic_ps_user_timeout;
710 
711 		if (beaconint_us > latency) {
712 			local->ps_sdata = NULL;
713 		} else {
714 			struct ieee80211_bss *bss;
715 			int maxslp = 1;
716 			u8 dtimper;
717 
718 			bss = (void *)found->u.mgd.associated->priv;
719 			dtimper = bss->dtim_period;
720 
721 			/* If the TIM IE is invalid, pretend the value is 1 */
722 			if (!dtimper)
723 				dtimper = 1;
724 			else if (dtimper > 1)
725 				maxslp = min_t(int, dtimper,
726 						    latency / beaconint_us);
727 
728 			local->hw.conf.max_sleep_period = maxslp;
729 			local->hw.conf.ps_dtim_period = dtimper;
730 			local->ps_sdata = found;
731 		}
732 	} else {
733 		local->ps_sdata = NULL;
734 	}
735 
736  change:
737 	ieee80211_change_ps(local);
738 }
739 
ieee80211_dynamic_ps_disable_work(struct work_struct * work)740 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
741 {
742 	struct ieee80211_local *local =
743 		container_of(work, struct ieee80211_local,
744 			     dynamic_ps_disable_work);
745 
746 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
747 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
748 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
749 	}
750 
751 	ieee80211_wake_queues_by_reason(&local->hw,
752 					IEEE80211_QUEUE_STOP_REASON_PS);
753 }
754 
ieee80211_dynamic_ps_enable_work(struct work_struct * work)755 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
756 {
757 	struct ieee80211_local *local =
758 		container_of(work, struct ieee80211_local,
759 			     dynamic_ps_enable_work);
760 	struct ieee80211_sub_if_data *sdata = local->ps_sdata;
761 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
762 
763 	/* can only happen when PS was just disabled anyway */
764 	if (!sdata)
765 		return;
766 
767 	if (local->hw.conf.flags & IEEE80211_CONF_PS)
768 		return;
769 
770 	if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
771 	    (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED))) {
772 		netif_tx_stop_all_queues(sdata->dev);
773 		/*
774 		 * Flush all the frames queued in the driver before
775 		 * going to power save
776 		 */
777 		drv_flush(local, false);
778 		ieee80211_send_nullfunc(local, sdata, 1);
779 
780 		/* Flush once again to get the tx status of nullfunc frame */
781 		drv_flush(local, false);
782 	}
783 
784 	if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
785 	      (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
786 	    (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
787 		ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
788 		local->hw.conf.flags |= IEEE80211_CONF_PS;
789 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
790 	}
791 
792 	netif_tx_start_all_queues(sdata->dev);
793 }
794 
ieee80211_dynamic_ps_timer(unsigned long data)795 void ieee80211_dynamic_ps_timer(unsigned long data)
796 {
797 	struct ieee80211_local *local = (void *) data;
798 
799 	if (local->quiescing || local->suspended)
800 		return;
801 
802 	ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
803 }
804 
805 /* MLME */
ieee80211_sta_wmm_params(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,u8 * wmm_param,size_t wmm_param_len)806 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
807 				     struct ieee80211_sub_if_data *sdata,
808 				     u8 *wmm_param, size_t wmm_param_len)
809 {
810 	struct ieee80211_tx_queue_params params;
811 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
812 	size_t left;
813 	int count;
814 	u8 *pos, uapsd_queues = 0;
815 
816 	if (!local->ops->conf_tx)
817 		return;
818 
819 	if (local->hw.queues < 4)
820 		return;
821 
822 	if (!wmm_param)
823 		return;
824 
825 	if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
826 		return;
827 
828 	if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
829 		uapsd_queues = local->uapsd_queues;
830 
831 	count = wmm_param[6] & 0x0f;
832 	if (count == ifmgd->wmm_last_param_set)
833 		return;
834 	ifmgd->wmm_last_param_set = count;
835 
836 	pos = wmm_param + 8;
837 	left = wmm_param_len - 8;
838 
839 	memset(&params, 0, sizeof(params));
840 
841 	local->wmm_acm = 0;
842 	for (; left >= 4; left -= 4, pos += 4) {
843 		int aci = (pos[0] >> 5) & 0x03;
844 		int acm = (pos[0] >> 4) & 0x01;
845 		bool uapsd = false;
846 		int queue;
847 
848 		switch (aci) {
849 		case 1: /* AC_BK */
850 			queue = 3;
851 			if (acm)
852 				local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
853 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
854 				uapsd = true;
855 			break;
856 		case 2: /* AC_VI */
857 			queue = 1;
858 			if (acm)
859 				local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
860 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
861 				uapsd = true;
862 			break;
863 		case 3: /* AC_VO */
864 			queue = 0;
865 			if (acm)
866 				local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
867 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
868 				uapsd = true;
869 			break;
870 		case 0: /* AC_BE */
871 		default:
872 			queue = 2;
873 			if (acm)
874 				local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
875 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
876 				uapsd = true;
877 			break;
878 		}
879 
880 		params.aifs = pos[0] & 0x0f;
881 		params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
882 		params.cw_min = ecw2cw(pos[1] & 0x0f);
883 		params.txop = get_unaligned_le16(pos + 2);
884 		params.uapsd = uapsd;
885 
886 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
887 		wiphy_debug(local->hw.wiphy,
888 			    "WMM queue=%d aci=%d acm=%d aifs=%d "
889 			    "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
890 			    queue, aci, acm,
891 			    params.aifs, params.cw_min, params.cw_max,
892 			    params.txop, params.uapsd);
893 #endif
894 		if (drv_conf_tx(local, queue, &params))
895 			wiphy_debug(local->hw.wiphy,
896 				    "failed to set TX queue parameters for queue %d\n",
897 				    queue);
898 	}
899 
900 	/* enable WMM or activate new settings */
901 	sdata->vif.bss_conf.qos = true;
902 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
903 }
904 
ieee80211_handle_bss_capability(struct ieee80211_sub_if_data * sdata,u16 capab,bool erp_valid,u8 erp)905 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
906 					   u16 capab, bool erp_valid, u8 erp)
907 {
908 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
909 	u32 changed = 0;
910 	bool use_protection;
911 	bool use_short_preamble;
912 	bool use_short_slot;
913 
914 	if (erp_valid) {
915 		use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
916 		use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
917 	} else {
918 		use_protection = false;
919 		use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
920 	}
921 
922 	use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
923 	if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
924 		use_short_slot = true;
925 
926 	if (use_protection != bss_conf->use_cts_prot) {
927 		bss_conf->use_cts_prot = use_protection;
928 		changed |= BSS_CHANGED_ERP_CTS_PROT;
929 	}
930 
931 	if (use_short_preamble != bss_conf->use_short_preamble) {
932 		bss_conf->use_short_preamble = use_short_preamble;
933 		changed |= BSS_CHANGED_ERP_PREAMBLE;
934 	}
935 
936 	if (use_short_slot != bss_conf->use_short_slot) {
937 		bss_conf->use_short_slot = use_short_slot;
938 		changed |= BSS_CHANGED_ERP_SLOT;
939 	}
940 
941 	return changed;
942 }
943 
ieee80211_set_associated(struct ieee80211_sub_if_data * sdata,struct cfg80211_bss * cbss,u32 bss_info_changed)944 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
945 				     struct cfg80211_bss *cbss,
946 				     u32 bss_info_changed)
947 {
948 	struct ieee80211_bss *bss = (void *)cbss->priv;
949 	struct ieee80211_local *local = sdata->local;
950 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
951 
952 	bss_info_changed |= BSS_CHANGED_ASSOC;
953 	/* set timing information */
954 	bss_conf->beacon_int = cbss->beacon_interval;
955 	bss_conf->timestamp = cbss->tsf;
956 
957 	bss_info_changed |= BSS_CHANGED_BEACON_INT;
958 	bss_info_changed |= ieee80211_handle_bss_capability(sdata,
959 		cbss->capability, bss->has_erp_value, bss->erp_value);
960 
961 	sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
962 		IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
963 
964 	sdata->u.mgd.associated = cbss;
965 	memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
966 
967 	sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
968 
969 	/* just to be sure */
970 	sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
971 				IEEE80211_STA_BEACON_POLL);
972 
973 	ieee80211_led_assoc(local, 1);
974 
975 	if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
976 		bss_conf->dtim_period = bss->dtim_period;
977 	else
978 		bss_conf->dtim_period = 0;
979 
980 	bss_conf->assoc = 1;
981 	/*
982 	 * For now just always ask the driver to update the basic rateset
983 	 * when we have associated, we aren't checking whether it actually
984 	 * changed or not.
985 	 */
986 	bss_info_changed |= BSS_CHANGED_BASIC_RATES;
987 
988 	/* And the BSSID changed - we're associated now */
989 	bss_info_changed |= BSS_CHANGED_BSSID;
990 
991 	/* Tell the driver to monitor connection quality (if supported) */
992 	if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
993 	    bss_conf->cqm_rssi_thold)
994 		bss_info_changed |= BSS_CHANGED_CQM;
995 
996 	/* Enable ARP filtering */
997 	if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
998 		bss_conf->arp_filter_enabled = sdata->arp_filter_state;
999 		bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1000 	}
1001 
1002 	ieee80211_bss_info_change_notify(sdata, bss_info_changed);
1003 
1004 	mutex_lock(&local->iflist_mtx);
1005 	ieee80211_recalc_ps(local, -1);
1006 	ieee80211_recalc_smps(local);
1007 	mutex_unlock(&local->iflist_mtx);
1008 
1009 	netif_tx_start_all_queues(sdata->dev);
1010 	netif_carrier_on(sdata->dev);
1011 }
1012 
ieee80211_set_disassoc(struct ieee80211_sub_if_data * sdata,bool remove_sta,bool tx)1013 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1014 				   bool remove_sta, bool tx)
1015 {
1016 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1017 	struct ieee80211_local *local = sdata->local;
1018 	struct sta_info *sta;
1019 	u32 changed = 0, config_changed = 0;
1020 	u8 bssid[ETH_ALEN];
1021 
1022 	ASSERT_MGD_MTX(ifmgd);
1023 
1024 	if (WARN_ON(!ifmgd->associated))
1025 		return;
1026 
1027 	memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1028 
1029 	ifmgd->associated = NULL;
1030 	memset(ifmgd->bssid, 0, ETH_ALEN);
1031 
1032 	/*
1033 	 * we need to commit the associated = NULL change because the
1034 	 * scan code uses that to determine whether this iface should
1035 	 * go to/wake up from powersave or not -- and could otherwise
1036 	 * wake the queues erroneously.
1037 	 */
1038 	smp_mb();
1039 
1040 	/*
1041 	 * Thus, we can only afterwards stop the queues -- to account
1042 	 * for the case where another CPU is finishing a scan at this
1043 	 * time -- we don't want the scan code to enable queues.
1044 	 */
1045 
1046 	netif_tx_stop_all_queues(sdata->dev);
1047 	netif_carrier_off(sdata->dev);
1048 
1049 	mutex_lock(&local->sta_mtx);
1050 	sta = sta_info_get(sdata, bssid);
1051 	if (sta) {
1052 		set_sta_flags(sta, WLAN_STA_BLOCK_BA);
1053 		ieee80211_sta_tear_down_BA_sessions(sta, tx);
1054 	}
1055 	mutex_unlock(&local->sta_mtx);
1056 
1057 	changed |= ieee80211_reset_erp_info(sdata);
1058 
1059 	ieee80211_led_assoc(local, 0);
1060 	changed |= BSS_CHANGED_ASSOC;
1061 	sdata->vif.bss_conf.assoc = false;
1062 
1063 	ieee80211_set_wmm_default(sdata);
1064 
1065 	/* channel(_type) changes are handled by ieee80211_hw_config */
1066 	WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
1067 
1068 	/* on the next assoc, re-program HT parameters */
1069 	sdata->ht_opmode_valid = false;
1070 
1071 	local->power_constr_level = 0;
1072 
1073 	del_timer_sync(&local->dynamic_ps_timer);
1074 	cancel_work_sync(&local->dynamic_ps_enable_work);
1075 
1076 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1077 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1078 		config_changed |= IEEE80211_CONF_CHANGE_PS;
1079 	}
1080 
1081 	ieee80211_hw_config(local, config_changed);
1082 
1083 	/* Disable ARP filtering */
1084 	if (sdata->vif.bss_conf.arp_filter_enabled) {
1085 		sdata->vif.bss_conf.arp_filter_enabled = false;
1086 		changed |= BSS_CHANGED_ARP_FILTER;
1087 	}
1088 
1089 	/* The BSSID (not really interesting) and HT changed */
1090 	changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1091 	ieee80211_bss_info_change_notify(sdata, changed);
1092 
1093 	if (remove_sta)
1094 		sta_info_destroy_addr(sdata, bssid);
1095 
1096 	del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1097 	del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1098 	del_timer_sync(&sdata->u.mgd.timer);
1099 	del_timer_sync(&sdata->u.mgd.chswitch_timer);
1100 }
1101 
ieee80211_sta_rx_notify(struct ieee80211_sub_if_data * sdata,struct ieee80211_hdr * hdr)1102 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1103 			     struct ieee80211_hdr *hdr)
1104 {
1105 	/*
1106 	 * We can postpone the mgd.timer whenever receiving unicast frames
1107 	 * from AP because we know that the connection is working both ways
1108 	 * at that time. But multicast frames (and hence also beacons) must
1109 	 * be ignored here, because we need to trigger the timer during
1110 	 * data idle periods for sending the periodic probe request to the
1111 	 * AP we're connected to.
1112 	 */
1113 	if (is_multicast_ether_addr(hdr->addr1))
1114 		return;
1115 
1116 	ieee80211_sta_reset_conn_monitor(sdata);
1117 }
1118 
ieee80211_reset_ap_probe(struct ieee80211_sub_if_data * sdata)1119 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1120 {
1121 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1122 
1123 	if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1124 			      IEEE80211_STA_CONNECTION_POLL)))
1125 	    return;
1126 
1127 	ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1128 			  IEEE80211_STA_BEACON_POLL);
1129 	mutex_lock(&sdata->local->iflist_mtx);
1130 	ieee80211_recalc_ps(sdata->local, -1);
1131 	mutex_unlock(&sdata->local->iflist_mtx);
1132 
1133 	if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1134 		return;
1135 
1136 	/*
1137 	 * We've received a probe response, but are not sure whether
1138 	 * we have or will be receiving any beacons or data, so let's
1139 	 * schedule the timers again, just in case.
1140 	 */
1141 	ieee80211_sta_reset_beacon_monitor(sdata);
1142 
1143 	mod_timer(&ifmgd->conn_mon_timer,
1144 		  round_jiffies_up(jiffies +
1145 				   IEEE80211_CONNECTION_IDLE_TIME));
1146 }
1147 
ieee80211_sta_tx_notify(struct ieee80211_sub_if_data * sdata,struct ieee80211_hdr * hdr,bool ack)1148 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
1149 			     struct ieee80211_hdr *hdr, bool ack)
1150 {
1151 	if (!ieee80211_is_data(hdr->frame_control))
1152 	    return;
1153 
1154 	if (ack)
1155 		ieee80211_sta_reset_conn_monitor(sdata);
1156 
1157 	if (ieee80211_is_nullfunc(hdr->frame_control) &&
1158 	    sdata->u.mgd.probe_send_count > 0) {
1159 		if (ack)
1160 			sdata->u.mgd.probe_send_count = 0;
1161 		else
1162 			sdata->u.mgd.nullfunc_failed = true;
1163 		ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1164 	}
1165 }
1166 
ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data * sdata)1167 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1168 {
1169 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1170 	const u8 *ssid;
1171 	u8 *dst = ifmgd->associated->bssid;
1172 	u8 unicast_limit = max(1, max_probe_tries - 3);
1173 
1174 	/*
1175 	 * Try sending broadcast probe requests for the last three
1176 	 * probe requests after the first ones failed since some
1177 	 * buggy APs only support broadcast probe requests.
1178 	 */
1179 	if (ifmgd->probe_send_count >= unicast_limit)
1180 		dst = NULL;
1181 
1182 	/*
1183 	 * When the hardware reports an accurate Tx ACK status, it's
1184 	 * better to send a nullfunc frame instead of a probe request,
1185 	 * as it will kick us off the AP quickly if we aren't associated
1186 	 * anymore. The timeout will be reset if the frame is ACKed by
1187 	 * the AP.
1188 	 */
1189 	if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1190 		ifmgd->nullfunc_failed = false;
1191 		ieee80211_send_nullfunc(sdata->local, sdata, 0);
1192 	} else {
1193 		ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1194 		ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
1195 	}
1196 
1197 	ifmgd->probe_send_count++;
1198 	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
1199 	run_again(ifmgd, ifmgd->probe_timeout);
1200 }
1201 
ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data * sdata,bool beacon)1202 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1203 				   bool beacon)
1204 {
1205 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1206 	bool already = false;
1207 
1208 	if (!ieee80211_sdata_running(sdata))
1209 		return;
1210 
1211 	if (sdata->local->scanning)
1212 		return;
1213 
1214 	if (sdata->local->tmp_channel)
1215 		return;
1216 
1217 	mutex_lock(&ifmgd->mtx);
1218 
1219 	if (!ifmgd->associated)
1220 		goto out;
1221 
1222 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1223 	if (beacon && net_ratelimit())
1224 		printk(KERN_DEBUG "%s: detected beacon loss from AP "
1225 		       "- sending probe request\n", sdata->name);
1226 #endif
1227 
1228 	/*
1229 	 * The driver/our work has already reported this event or the
1230 	 * connection monitoring has kicked in and we have already sent
1231 	 * a probe request. Or maybe the AP died and the driver keeps
1232 	 * reporting until we disassociate...
1233 	 *
1234 	 * In either case we have to ignore the current call to this
1235 	 * function (except for setting the correct probe reason bit)
1236 	 * because otherwise we would reset the timer every time and
1237 	 * never check whether we received a probe response!
1238 	 */
1239 	if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1240 			    IEEE80211_STA_CONNECTION_POLL))
1241 		already = true;
1242 
1243 	if (beacon)
1244 		ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1245 	else
1246 		ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1247 
1248 	if (already)
1249 		goto out;
1250 
1251 	mutex_lock(&sdata->local->iflist_mtx);
1252 	ieee80211_recalc_ps(sdata->local, -1);
1253 	mutex_unlock(&sdata->local->iflist_mtx);
1254 
1255 	ifmgd->probe_send_count = 0;
1256 	ieee80211_mgd_probe_ap_send(sdata);
1257  out:
1258 	mutex_unlock(&ifmgd->mtx);
1259 }
1260 
ieee80211_ap_probereq_get(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1261 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1262 					  struct ieee80211_vif *vif)
1263 {
1264 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1265 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1266 	struct sk_buff *skb;
1267 	const u8 *ssid;
1268 
1269 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1270 		return NULL;
1271 
1272 	ASSERT_MGD_MTX(ifmgd);
1273 
1274 	if (!ifmgd->associated)
1275 		return NULL;
1276 
1277 	ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1278 	skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
1279 					ssid + 2, ssid[1], NULL, 0);
1280 
1281 	return skb;
1282 }
1283 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1284 
__ieee80211_connection_loss(struct ieee80211_sub_if_data * sdata)1285 static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1286 {
1287 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1288 	struct ieee80211_local *local = sdata->local;
1289 	u8 bssid[ETH_ALEN];
1290 
1291 	mutex_lock(&ifmgd->mtx);
1292 	if (!ifmgd->associated) {
1293 		mutex_unlock(&ifmgd->mtx);
1294 		return;
1295 	}
1296 
1297 	memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1298 
1299 	printk(KERN_DEBUG "%s: Connection to AP %pM lost.\n",
1300 	       sdata->name, bssid);
1301 
1302 	ieee80211_set_disassoc(sdata, true, true);
1303 	mutex_unlock(&ifmgd->mtx);
1304 
1305 	mutex_lock(&local->mtx);
1306 	ieee80211_recalc_idle(local);
1307 	mutex_unlock(&local->mtx);
1308 	/*
1309 	 * must be outside lock due to cfg80211,
1310 	 * but that's not a problem.
1311 	 */
1312 	ieee80211_send_deauth_disassoc(sdata, bssid,
1313 				       IEEE80211_STYPE_DEAUTH,
1314 				       WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1315 				       NULL, true);
1316 }
1317 
ieee80211_beacon_connection_loss_work(struct work_struct * work)1318 void ieee80211_beacon_connection_loss_work(struct work_struct *work)
1319 {
1320 	struct ieee80211_sub_if_data *sdata =
1321 		container_of(work, struct ieee80211_sub_if_data,
1322 			     u.mgd.beacon_connection_loss_work);
1323 
1324 	if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1325 		__ieee80211_connection_loss(sdata);
1326 	else
1327 		ieee80211_mgd_probe_ap(sdata, true);
1328 }
1329 
ieee80211_beacon_loss(struct ieee80211_vif * vif)1330 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1331 {
1332 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1333 	struct ieee80211_hw *hw = &sdata->local->hw;
1334 
1335 	trace_api_beacon_loss(sdata);
1336 
1337 	WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1338 	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1339 }
1340 EXPORT_SYMBOL(ieee80211_beacon_loss);
1341 
ieee80211_connection_loss(struct ieee80211_vif * vif)1342 void ieee80211_connection_loss(struct ieee80211_vif *vif)
1343 {
1344 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1345 	struct ieee80211_hw *hw = &sdata->local->hw;
1346 
1347 	trace_api_connection_loss(sdata);
1348 
1349 	WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1350 	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1351 }
1352 EXPORT_SYMBOL(ieee80211_connection_loss);
1353 
1354 
1355 static enum rx_mgmt_action __must_check
ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)1356 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1357 			 struct ieee80211_mgmt *mgmt, size_t len)
1358 {
1359 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1360 	const u8 *bssid = NULL;
1361 	u16 reason_code;
1362 
1363 	if (len < 24 + 2)
1364 		return RX_MGMT_NONE;
1365 
1366 	ASSERT_MGD_MTX(ifmgd);
1367 
1368 	bssid = ifmgd->associated->bssid;
1369 
1370 	reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1371 
1372 	printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
1373 			sdata->name, bssid, reason_code);
1374 
1375 	ieee80211_set_disassoc(sdata, true, false);
1376 	mutex_lock(&sdata->local->mtx);
1377 	ieee80211_recalc_idle(sdata->local);
1378 	mutex_unlock(&sdata->local->mtx);
1379 
1380 	return RX_MGMT_CFG80211_DEAUTH;
1381 }
1382 
1383 
1384 static enum rx_mgmt_action __must_check
ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)1385 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1386 			   struct ieee80211_mgmt *mgmt, size_t len)
1387 {
1388 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1389 	u16 reason_code;
1390 
1391 	if (len < 24 + 2)
1392 		return RX_MGMT_NONE;
1393 
1394 	ASSERT_MGD_MTX(ifmgd);
1395 
1396 	if (WARN_ON(!ifmgd->associated))
1397 		return RX_MGMT_NONE;
1398 
1399 	if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
1400 		return RX_MGMT_NONE;
1401 
1402 	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1403 
1404 	printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
1405 			sdata->name, mgmt->sa, reason_code);
1406 
1407 	ieee80211_set_disassoc(sdata, true, false);
1408 	mutex_lock(&sdata->local->mtx);
1409 	ieee80211_recalc_idle(sdata->local);
1410 	mutex_unlock(&sdata->local->mtx);
1411 	return RX_MGMT_CFG80211_DISASSOC;
1412 }
1413 
1414 
ieee80211_assoc_success(struct ieee80211_work * wk,struct ieee80211_mgmt * mgmt,size_t len)1415 static bool ieee80211_assoc_success(struct ieee80211_work *wk,
1416 				    struct ieee80211_mgmt *mgmt, size_t len)
1417 {
1418 	struct ieee80211_sub_if_data *sdata = wk->sdata;
1419 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1420 	struct ieee80211_local *local = sdata->local;
1421 	struct ieee80211_supported_band *sband;
1422 	struct sta_info *sta;
1423 	struct cfg80211_bss *cbss = wk->assoc.bss;
1424 	u8 *pos;
1425 	u32 rates, basic_rates;
1426 	u16 capab_info, aid;
1427 	struct ieee802_11_elems elems;
1428 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1429 	u32 changed = 0;
1430 	int i, j, err;
1431 	bool have_higher_than_11mbit = false;
1432 	u16 ap_ht_cap_flags;
1433 
1434 	/* AssocResp and ReassocResp have identical structure */
1435 
1436 	aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1437 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1438 
1439 	if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1440 		printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1441 		       "set\n", sdata->name, aid);
1442 	aid &= ~(BIT(15) | BIT(14));
1443 
1444 	pos = mgmt->u.assoc_resp.variable;
1445 	ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1446 
1447 	if (!elems.supp_rates) {
1448 		printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1449 		       sdata->name);
1450 		return false;
1451 	}
1452 
1453 	ifmgd->aid = aid;
1454 
1455 	sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
1456 	if (!sta) {
1457 		printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1458 		       " the AP\n", sdata->name);
1459 		return false;
1460 	}
1461 
1462 	set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1463 			   WLAN_STA_ASSOC_AP);
1464 	if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1465 		set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1466 
1467 	rates = 0;
1468 	basic_rates = 0;
1469 	sband = local->hw.wiphy->bands[wk->chan->band];
1470 
1471 	for (i = 0; i < elems.supp_rates_len; i++) {
1472 		int rate = (elems.supp_rates[i] & 0x7f) * 5;
1473 		bool is_basic = !!(elems.supp_rates[i] & 0x80);
1474 
1475 		if (rate > 110)
1476 			have_higher_than_11mbit = true;
1477 
1478 		for (j = 0; j < sband->n_bitrates; j++) {
1479 			if (sband->bitrates[j].bitrate == rate) {
1480 				rates |= BIT(j);
1481 				if (is_basic)
1482 					basic_rates |= BIT(j);
1483 				break;
1484 			}
1485 		}
1486 	}
1487 
1488 	for (i = 0; i < elems.ext_supp_rates_len; i++) {
1489 		int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1490 		bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
1491 
1492 		if (rate > 110)
1493 			have_higher_than_11mbit = true;
1494 
1495 		for (j = 0; j < sband->n_bitrates; j++) {
1496 			if (sband->bitrates[j].bitrate == rate) {
1497 				rates |= BIT(j);
1498 				if (is_basic)
1499 					basic_rates |= BIT(j);
1500 				break;
1501 			}
1502 		}
1503 	}
1504 
1505 	sta->sta.supp_rates[wk->chan->band] = rates;
1506 	sdata->vif.bss_conf.basic_rates = basic_rates;
1507 
1508 	/* cf. IEEE 802.11 9.2.12 */
1509 	if (wk->chan->band == IEEE80211_BAND_2GHZ &&
1510 	    have_higher_than_11mbit)
1511 		sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1512 	else
1513 		sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1514 
1515 	if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1516 		ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1517 				elems.ht_cap_elem, &sta->sta.ht_cap);
1518 
1519 	ap_ht_cap_flags = sta->sta.ht_cap.cap;
1520 
1521 	rate_control_rate_init(sta);
1522 
1523 	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
1524 		set_sta_flags(sta, WLAN_STA_MFP);
1525 
1526 	if (elems.wmm_param)
1527 		set_sta_flags(sta, WLAN_STA_WME);
1528 
1529 	err = sta_info_insert(sta);
1530 	sta = NULL;
1531 	if (err) {
1532 		printk(KERN_DEBUG "%s: failed to insert STA entry for"
1533 		       " the AP (error %d)\n", sdata->name, err);
1534 		return false;
1535 	}
1536 
1537 	/*
1538 	 * Always handle WMM once after association regardless
1539 	 * of the first value the AP uses. Setting -1 here has
1540 	 * that effect because the AP values is an unsigned
1541 	 * 4-bit value.
1542 	 */
1543 	ifmgd->wmm_last_param_set = -1;
1544 
1545 	if (elems.wmm_param)
1546 		ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
1547 					 elems.wmm_param_len);
1548 	else
1549 		ieee80211_set_wmm_default(sdata);
1550 
1551 	local->oper_channel = wk->chan;
1552 
1553 	if (elems.ht_info_elem && elems.wmm_param &&
1554 	    (sdata->local->hw.queues >= 4) &&
1555 	    !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1556 		changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1557 					       cbss->bssid, ap_ht_cap_flags);
1558 
1559 	/* set AID and assoc capability,
1560 	 * ieee80211_set_associated() will tell the driver */
1561 	bss_conf->aid = aid;
1562 	bss_conf->assoc_capability = capab_info;
1563 	ieee80211_set_associated(sdata, cbss, changed);
1564 
1565 	/*
1566 	 * If we're using 4-addr mode, let the AP know that we're
1567 	 * doing so, so that it can create the STA VLAN on its side
1568 	 */
1569 	if (ifmgd->use_4addr)
1570 		ieee80211_send_4addr_nullfunc(local, sdata);
1571 
1572 	/*
1573 	 * Start timer to probe the connection to the AP now.
1574 	 * Also start the timer that will detect beacon loss.
1575 	 */
1576 	ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
1577 	ieee80211_sta_reset_beacon_monitor(sdata);
1578 
1579 	return true;
1580 }
1581 
1582 
ieee80211_rx_bss_info(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status,struct ieee802_11_elems * elems,bool beacon)1583 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1584 				  struct ieee80211_mgmt *mgmt,
1585 				  size_t len,
1586 				  struct ieee80211_rx_status *rx_status,
1587 				  struct ieee802_11_elems *elems,
1588 				  bool beacon)
1589 {
1590 	struct ieee80211_local *local = sdata->local;
1591 	int freq;
1592 	struct ieee80211_bss *bss;
1593 	struct ieee80211_channel *channel;
1594 	bool need_ps = false;
1595 
1596 	if (sdata->u.mgd.associated) {
1597 		bss = (void *)sdata->u.mgd.associated->priv;
1598 		/* not previously set so we may need to recalc */
1599 		need_ps = !bss->dtim_period;
1600 	}
1601 
1602 	if (elems->ds_params && elems->ds_params_len == 1)
1603 		freq = ieee80211_channel_to_frequency(elems->ds_params[0],
1604 						      rx_status->band);
1605 	else
1606 		freq = rx_status->freq;
1607 
1608 	channel = ieee80211_get_channel(local->hw.wiphy, freq);
1609 
1610 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1611 		return;
1612 
1613 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1614 					channel, beacon);
1615 	if (bss)
1616 		ieee80211_rx_bss_put(local, bss);
1617 
1618 	if (!sdata->u.mgd.associated)
1619 		return;
1620 
1621 	if (need_ps) {
1622 		mutex_lock(&local->iflist_mtx);
1623 		ieee80211_recalc_ps(local, -1);
1624 		mutex_unlock(&local->iflist_mtx);
1625 	}
1626 
1627 	if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
1628 	    (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
1629 							ETH_ALEN) == 0)) {
1630 		struct ieee80211_channel_sw_ie *sw_elem =
1631 			(struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
1632 		ieee80211_sta_process_chanswitch(sdata, sw_elem,
1633 						 bss, rx_status->mactime);
1634 	}
1635 }
1636 
1637 
ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1638 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1639 					 struct sk_buff *skb)
1640 {
1641 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1642 	struct ieee80211_if_managed *ifmgd;
1643 	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
1644 	size_t baselen, len = skb->len;
1645 	struct ieee802_11_elems elems;
1646 
1647 	ifmgd = &sdata->u.mgd;
1648 
1649 	ASSERT_MGD_MTX(ifmgd);
1650 
1651 	if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
1652 		return; /* ignore ProbeResp to foreign address */
1653 
1654 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1655 	if (baselen > len)
1656 		return;
1657 
1658 	ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1659 				&elems);
1660 
1661 	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1662 
1663 	if (ifmgd->associated &&
1664 	    memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
1665 		ieee80211_reset_ap_probe(sdata);
1666 }
1667 
1668 /*
1669  * This is the canonical list of information elements we care about,
1670  * the filter code also gives us all changes to the Microsoft OUI
1671  * (00:50:F2) vendor IE which is used for WMM which we need to track.
1672  *
1673  * We implement beacon filtering in software since that means we can
1674  * avoid processing the frame here and in cfg80211, and userspace
1675  * will not be able to tell whether the hardware supports it or not.
1676  *
1677  * XXX: This list needs to be dynamic -- userspace needs to be able to
1678  *	add items it requires. It also needs to be able to tell us to
1679  *	look out for other vendor IEs.
1680  */
1681 static const u64 care_about_ies =
1682 	(1ULL << WLAN_EID_COUNTRY) |
1683 	(1ULL << WLAN_EID_ERP_INFO) |
1684 	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
1685 	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
1686 	(1ULL << WLAN_EID_HT_CAPABILITY) |
1687 	(1ULL << WLAN_EID_HT_INFORMATION);
1688 
ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status)1689 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1690 				     struct ieee80211_mgmt *mgmt,
1691 				     size_t len,
1692 				     struct ieee80211_rx_status *rx_status)
1693 {
1694 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1695 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1696 	size_t baselen;
1697 	struct ieee802_11_elems elems;
1698 	struct ieee80211_local *local = sdata->local;
1699 	u32 changed = 0;
1700 	bool erp_valid, directed_tim = false;
1701 	u8 erp_value = 0;
1702 	u32 ncrc;
1703 	u8 *bssid;
1704 
1705 	ASSERT_MGD_MTX(ifmgd);
1706 
1707 	/* Process beacon from the current BSS */
1708 	baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1709 	if (baselen > len)
1710 		return;
1711 
1712 	if (rx_status->freq != local->hw.conf.channel->center_freq)
1713 		return;
1714 
1715 	/*
1716 	 * We might have received a number of frames, among them a
1717 	 * disassoc frame and a beacon...
1718 	 */
1719 	if (!ifmgd->associated)
1720 		return;
1721 
1722 	bssid = ifmgd->associated->bssid;
1723 
1724 	/*
1725 	 * And in theory even frames from a different AP we were just
1726 	 * associated to a split-second ago!
1727 	 */
1728 	if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
1729 		return;
1730 
1731 	/* Track average RSSI from the Beacon frames of the current AP */
1732 	ifmgd->last_beacon_signal = rx_status->signal;
1733 	if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
1734 		ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
1735 		ifmgd->ave_beacon_signal = rx_status->signal * 16;
1736 		ifmgd->last_cqm_event_signal = 0;
1737 		ifmgd->count_beacon_signal = 1;
1738 	} else {
1739 		ifmgd->ave_beacon_signal =
1740 			(IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
1741 			 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
1742 			 ifmgd->ave_beacon_signal) / 16;
1743 		ifmgd->count_beacon_signal++;
1744 	}
1745 	if (bss_conf->cqm_rssi_thold &&
1746 	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
1747 	    !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1748 		int sig = ifmgd->ave_beacon_signal / 16;
1749 		int last_event = ifmgd->last_cqm_event_signal;
1750 		int thold = bss_conf->cqm_rssi_thold;
1751 		int hyst = bss_conf->cqm_rssi_hyst;
1752 		if (sig < thold &&
1753 		    (last_event == 0 || sig < last_event - hyst)) {
1754 			ifmgd->last_cqm_event_signal = sig;
1755 			ieee80211_cqm_rssi_notify(
1756 				&sdata->vif,
1757 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
1758 				GFP_KERNEL);
1759 		} else if (sig > thold &&
1760 			   (last_event == 0 || sig > last_event + hyst)) {
1761 			ifmgd->last_cqm_event_signal = sig;
1762 			ieee80211_cqm_rssi_notify(
1763 				&sdata->vif,
1764 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
1765 				GFP_KERNEL);
1766 		}
1767 	}
1768 
1769 	if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
1770 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1771 		if (net_ratelimit()) {
1772 			printk(KERN_DEBUG "%s: cancelling probereq poll due "
1773 			       "to a received beacon\n", sdata->name);
1774 		}
1775 #endif
1776 		ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
1777 		mutex_lock(&local->iflist_mtx);
1778 		ieee80211_recalc_ps(local, -1);
1779 		mutex_unlock(&local->iflist_mtx);
1780 	}
1781 
1782 	/*
1783 	 * Push the beacon loss detection into the future since
1784 	 * we are processing a beacon from the AP just now.
1785 	 */
1786 	ieee80211_sta_reset_beacon_monitor(sdata);
1787 
1788 	ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1789 	ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1790 					  len - baselen, &elems,
1791 					  care_about_ies, ncrc);
1792 
1793 	if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1794 		directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1795 						   ifmgd->aid);
1796 
1797 	if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
1798 		ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1799 				      true);
1800 
1801 		ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
1802 					 elems.wmm_param_len);
1803 	}
1804 
1805 	if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1806 		if (directed_tim) {
1807 			if (local->hw.conf.dynamic_ps_timeout > 0) {
1808 				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1809 				ieee80211_hw_config(local,
1810 						    IEEE80211_CONF_CHANGE_PS);
1811 				ieee80211_send_nullfunc(local, sdata, 0);
1812 			} else {
1813 				local->pspolling = true;
1814 
1815 				/*
1816 				 * Here is assumed that the driver will be
1817 				 * able to send ps-poll frame and receive a
1818 				 * response even though power save mode is
1819 				 * enabled, but some drivers might require
1820 				 * to disable power save here. This needs
1821 				 * to be investigated.
1822 				 */
1823 				ieee80211_send_pspoll(local, sdata);
1824 			}
1825 		}
1826 	}
1827 
1828 	if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
1829 		return;
1830 	ifmgd->beacon_crc = ncrc;
1831 	ifmgd->beacon_crc_valid = true;
1832 
1833 	if (elems.erp_info && elems.erp_info_len >= 1) {
1834 		erp_valid = true;
1835 		erp_value = elems.erp_info[0];
1836 	} else {
1837 		erp_valid = false;
1838 	}
1839 	changed |= ieee80211_handle_bss_capability(sdata,
1840 			le16_to_cpu(mgmt->u.beacon.capab_info),
1841 			erp_valid, erp_value);
1842 
1843 
1844 	if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1845 	    !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
1846 		struct sta_info *sta;
1847 		struct ieee80211_supported_band *sband;
1848 		u16 ap_ht_cap_flags;
1849 
1850 		rcu_read_lock();
1851 
1852 		sta = sta_info_get(sdata, bssid);
1853 		if (WARN_ON(!sta)) {
1854 			rcu_read_unlock();
1855 			return;
1856 		}
1857 
1858 		sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1859 
1860 		ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1861 				elems.ht_cap_elem, &sta->sta.ht_cap);
1862 
1863 		ap_ht_cap_flags = sta->sta.ht_cap.cap;
1864 
1865 		rcu_read_unlock();
1866 
1867 		changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1868 					       bssid, ap_ht_cap_flags);
1869 	}
1870 
1871 	/* Note: country IE parsing is done for us by cfg80211 */
1872 	if (elems.country_elem) {
1873 		/* TODO: IBSS also needs this */
1874 		if (elems.pwr_constr_elem)
1875 			ieee80211_handle_pwr_constr(sdata,
1876 				le16_to_cpu(mgmt->u.probe_resp.capab_info),
1877 				elems.pwr_constr_elem,
1878 				elems.pwr_constr_elem_len);
1879 	}
1880 
1881 	ieee80211_bss_info_change_notify(sdata, changed);
1882 }
1883 
ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1884 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1885 				  struct sk_buff *skb)
1886 {
1887 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1888 	struct ieee80211_rx_status *rx_status;
1889 	struct ieee80211_mgmt *mgmt;
1890 	enum rx_mgmt_action rma = RX_MGMT_NONE;
1891 	u16 fc;
1892 
1893 	rx_status = (struct ieee80211_rx_status *) skb->cb;
1894 	mgmt = (struct ieee80211_mgmt *) skb->data;
1895 	fc = le16_to_cpu(mgmt->frame_control);
1896 
1897 	mutex_lock(&ifmgd->mtx);
1898 
1899 	if (ifmgd->associated &&
1900 	    memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
1901 		switch (fc & IEEE80211_FCTL_STYPE) {
1902 		case IEEE80211_STYPE_BEACON:
1903 			ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1904 						 rx_status);
1905 			break;
1906 		case IEEE80211_STYPE_PROBE_RESP:
1907 			ieee80211_rx_mgmt_probe_resp(sdata, skb);
1908 			break;
1909 		case IEEE80211_STYPE_DEAUTH:
1910 			rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
1911 			break;
1912 		case IEEE80211_STYPE_DISASSOC:
1913 			rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1914 			break;
1915 		case IEEE80211_STYPE_ACTION:
1916 			switch (mgmt->u.action.category) {
1917 			case WLAN_CATEGORY_SPECTRUM_MGMT:
1918 				ieee80211_sta_process_chanswitch(sdata,
1919 						&mgmt->u.action.u.chan_switch.sw_elem,
1920 						(void *)ifmgd->associated->priv,
1921 						rx_status->mactime);
1922 				break;
1923 			}
1924 		}
1925 		mutex_unlock(&ifmgd->mtx);
1926 
1927 		switch (rma) {
1928 		case RX_MGMT_NONE:
1929 			/* no action */
1930 			break;
1931 		case RX_MGMT_CFG80211_DEAUTH:
1932 			cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
1933 			break;
1934 		case RX_MGMT_CFG80211_DISASSOC:
1935 			cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
1936 			break;
1937 		default:
1938 			WARN(1, "unexpected: %d", rma);
1939 		}
1940 		return;
1941 	}
1942 
1943 	mutex_unlock(&ifmgd->mtx);
1944 
1945 	if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
1946 	    (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
1947 		struct ieee80211_local *local = sdata->local;
1948 		struct ieee80211_work *wk;
1949 
1950 		mutex_lock(&local->mtx);
1951 		list_for_each_entry(wk, &local->work_list, list) {
1952 			if (wk->sdata != sdata)
1953 				continue;
1954 
1955 			if (wk->type != IEEE80211_WORK_ASSOC &&
1956 			    wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
1957 				continue;
1958 
1959 			if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
1960 				continue;
1961 			if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
1962 				continue;
1963 
1964 			/*
1965 			 * Printing the message only here means we can't
1966 			 * spuriously print it, but it also means that it
1967 			 * won't be printed when the frame comes in before
1968 			 * we even tried to associate or in similar cases.
1969 			 *
1970 			 * Ultimately, I suspect cfg80211 should print the
1971 			 * messages instead.
1972 			 */
1973 			printk(KERN_DEBUG
1974 			       "%s: deauthenticated from %pM (Reason: %u)\n",
1975 			       sdata->name, mgmt->bssid,
1976 			       le16_to_cpu(mgmt->u.deauth.reason_code));
1977 
1978 			list_del_rcu(&wk->list);
1979 			free_work(wk);
1980 			break;
1981 		}
1982 		mutex_unlock(&local->mtx);
1983 
1984 		cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
1985 	}
1986 }
1987 
ieee80211_sta_timer(unsigned long data)1988 static void ieee80211_sta_timer(unsigned long data)
1989 {
1990 	struct ieee80211_sub_if_data *sdata =
1991 		(struct ieee80211_sub_if_data *) data;
1992 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1993 	struct ieee80211_local *local = sdata->local;
1994 
1995 	if (local->quiescing) {
1996 		set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
1997 		return;
1998 	}
1999 
2000 	ieee80211_queue_work(&local->hw, &sdata->work);
2001 }
2002 
ieee80211_sta_connection_lost(struct ieee80211_sub_if_data * sdata,u8 * bssid)2003 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
2004 					  u8 *bssid)
2005 {
2006 	struct ieee80211_local *local = sdata->local;
2007 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2008 
2009 	ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
2010 			  IEEE80211_STA_BEACON_POLL);
2011 
2012 	ieee80211_set_disassoc(sdata, true, true);
2013 	mutex_unlock(&ifmgd->mtx);
2014 	mutex_lock(&local->mtx);
2015 	ieee80211_recalc_idle(local);
2016 	mutex_unlock(&local->mtx);
2017 	/*
2018 	 * must be outside lock due to cfg80211,
2019 	 * but that's not a problem.
2020 	 */
2021 	ieee80211_send_deauth_disassoc(sdata, bssid,
2022 			IEEE80211_STYPE_DEAUTH,
2023 			WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2024 			NULL, true);
2025 	mutex_lock(&ifmgd->mtx);
2026 }
2027 
ieee80211_sta_work(struct ieee80211_sub_if_data * sdata)2028 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
2029 {
2030 	struct ieee80211_local *local = sdata->local;
2031 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2032 
2033 	/* then process the rest of the work */
2034 	mutex_lock(&ifmgd->mtx);
2035 
2036 	if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2037 			    IEEE80211_STA_CONNECTION_POLL) &&
2038 	    ifmgd->associated) {
2039 		u8 bssid[ETH_ALEN];
2040 		int max_tries;
2041 
2042 		memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
2043 
2044 		if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2045 			max_tries = max_nullfunc_tries;
2046 		else
2047 			max_tries = max_probe_tries;
2048 
2049 		/* ACK received for nullfunc probing frame */
2050 		if (!ifmgd->probe_send_count)
2051 			ieee80211_reset_ap_probe(sdata);
2052 		else if (ifmgd->nullfunc_failed) {
2053 			if (ifmgd->probe_send_count < max_tries) {
2054 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2055 				wiphy_debug(local->hw.wiphy,
2056 					    "%s: No ack for nullfunc frame to"
2057 					    " AP %pM, try %d/%i\n",
2058 					    sdata->name, bssid,
2059 					    ifmgd->probe_send_count, max_tries);
2060 #endif
2061 				ieee80211_mgd_probe_ap_send(sdata);
2062 			} else {
2063 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2064 				wiphy_debug(local->hw.wiphy,
2065 					    "%s: No ack for nullfunc frame to"
2066 					    " AP %pM, disconnecting.\n",
2067 					    sdata->name, bssid);
2068 #endif
2069 				ieee80211_sta_connection_lost(sdata, bssid);
2070 			}
2071 		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
2072 			run_again(ifmgd, ifmgd->probe_timeout);
2073 		else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2074 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2075 			wiphy_debug(local->hw.wiphy,
2076 				    "%s: Failed to send nullfunc to AP %pM"
2077 				    " after %dms, disconnecting.\n",
2078 				    sdata->name,
2079 				    bssid, probe_wait_ms);
2080 #endif
2081 			ieee80211_sta_connection_lost(sdata, bssid);
2082 		} else if (ifmgd->probe_send_count < max_tries) {
2083 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2084 			wiphy_debug(local->hw.wiphy,
2085 				    "%s: No probe response from AP %pM"
2086 				    " after %dms, try %d/%i\n",
2087 				    sdata->name,
2088 				    bssid, probe_wait_ms,
2089 				    ifmgd->probe_send_count, max_tries);
2090 #endif
2091 			ieee80211_mgd_probe_ap_send(sdata);
2092 		} else {
2093 			/*
2094 			 * We actually lost the connection ... or did we?
2095 			 * Let's make sure!
2096 			 */
2097 			wiphy_debug(local->hw.wiphy,
2098 				    "%s: No probe response from AP %pM"
2099 				    " after %dms, disconnecting.\n",
2100 				    sdata->name,
2101 				    bssid, probe_wait_ms);
2102 
2103 			ieee80211_sta_connection_lost(sdata, bssid);
2104 		}
2105 	}
2106 
2107 	mutex_unlock(&ifmgd->mtx);
2108 }
2109 
ieee80211_sta_bcn_mon_timer(unsigned long data)2110 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2111 {
2112 	struct ieee80211_sub_if_data *sdata =
2113 		(struct ieee80211_sub_if_data *) data;
2114 	struct ieee80211_local *local = sdata->local;
2115 
2116 	if (local->quiescing)
2117 		return;
2118 
2119 	ieee80211_queue_work(&sdata->local->hw,
2120 			     &sdata->u.mgd.beacon_connection_loss_work);
2121 }
2122 
ieee80211_sta_conn_mon_timer(unsigned long data)2123 static void ieee80211_sta_conn_mon_timer(unsigned long data)
2124 {
2125 	struct ieee80211_sub_if_data *sdata =
2126 		(struct ieee80211_sub_if_data *) data;
2127 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2128 	struct ieee80211_local *local = sdata->local;
2129 
2130 	if (local->quiescing)
2131 		return;
2132 
2133 	ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
2134 }
2135 
ieee80211_sta_monitor_work(struct work_struct * work)2136 static void ieee80211_sta_monitor_work(struct work_struct *work)
2137 {
2138 	struct ieee80211_sub_if_data *sdata =
2139 		container_of(work, struct ieee80211_sub_if_data,
2140 			     u.mgd.monitor_work);
2141 
2142 	ieee80211_mgd_probe_ap(sdata, false);
2143 }
2144 
ieee80211_restart_sta_timer(struct ieee80211_sub_if_data * sdata)2145 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2146 {
2147 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2148 		sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2149 					IEEE80211_STA_CONNECTION_POLL);
2150 
2151 		/* let's probe the connection once */
2152 		ieee80211_queue_work(&sdata->local->hw,
2153 			   &sdata->u.mgd.monitor_work);
2154 		/* and do all the other regular work too */
2155 		ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2156 	}
2157 }
2158 
2159 #ifdef CONFIG_PM
ieee80211_sta_quiesce(struct ieee80211_sub_if_data * sdata)2160 void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2161 {
2162 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2163 
2164 	/*
2165 	 * we need to use atomic bitops for the running bits
2166 	 * only because both timers might fire at the same
2167 	 * time -- the code here is properly synchronised.
2168 	 */
2169 
2170 	cancel_work_sync(&ifmgd->request_smps_work);
2171 
2172 	cancel_work_sync(&ifmgd->beacon_connection_loss_work);
2173 	if (del_timer_sync(&ifmgd->timer))
2174 		set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2175 
2176 	cancel_work_sync(&ifmgd->chswitch_work);
2177 	if (del_timer_sync(&ifmgd->chswitch_timer))
2178 		set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
2179 
2180 	cancel_work_sync(&ifmgd->monitor_work);
2181 	/* these will just be re-established on connection */
2182 	del_timer_sync(&ifmgd->conn_mon_timer);
2183 	del_timer_sync(&ifmgd->bcn_mon_timer);
2184 }
2185 
ieee80211_sta_restart(struct ieee80211_sub_if_data * sdata)2186 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2187 {
2188 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2189 
2190 	if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2191 		add_timer(&ifmgd->timer);
2192 	if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2193 		add_timer(&ifmgd->chswitch_timer);
2194 	ieee80211_sta_reset_beacon_monitor(sdata);
2195 	ieee80211_restart_sta_timer(sdata);
2196 }
2197 #endif
2198 
2199 /* interface setup */
ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data * sdata)2200 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2201 {
2202 	struct ieee80211_if_managed *ifmgd;
2203 
2204 	ifmgd = &sdata->u.mgd;
2205 	INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
2206 	INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
2207 	INIT_WORK(&ifmgd->beacon_connection_loss_work,
2208 		  ieee80211_beacon_connection_loss_work);
2209 	INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
2210 	setup_timer(&ifmgd->timer, ieee80211_sta_timer,
2211 		    (unsigned long) sdata);
2212 	setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2213 		    (unsigned long) sdata);
2214 	setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2215 		    (unsigned long) sdata);
2216 	setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
2217 		    (unsigned long) sdata);
2218 
2219 	ifmgd->flags = 0;
2220 
2221 	mutex_init(&ifmgd->mtx);
2222 
2223 	if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2224 		ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2225 	else
2226 		ifmgd->req_smps = IEEE80211_SMPS_OFF;
2227 }
2228 
2229 /* scan finished notification */
ieee80211_mlme_notify_scan_completed(struct ieee80211_local * local)2230 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2231 {
2232 	struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2233 
2234 	/* Restart STA timers */
2235 	rcu_read_lock();
2236 	list_for_each_entry_rcu(sdata, &local->interfaces, list)
2237 		ieee80211_restart_sta_timer(sdata);
2238 	rcu_read_unlock();
2239 }
2240 
ieee80211_max_network_latency(struct notifier_block * nb,unsigned long data,void * dummy)2241 int ieee80211_max_network_latency(struct notifier_block *nb,
2242 				  unsigned long data, void *dummy)
2243 {
2244 	s32 latency_usec = (s32) data;
2245 	struct ieee80211_local *local =
2246 		container_of(nb, struct ieee80211_local,
2247 			     network_latency_notifier);
2248 
2249 	mutex_lock(&local->iflist_mtx);
2250 	ieee80211_recalc_ps(local, latency_usec);
2251 	mutex_unlock(&local->iflist_mtx);
2252 
2253 	return 0;
2254 }
2255 
2256 /* config hooks */
2257 static enum work_done_result
ieee80211_probe_auth_done(struct ieee80211_work * wk,struct sk_buff * skb)2258 ieee80211_probe_auth_done(struct ieee80211_work *wk,
2259 			  struct sk_buff *skb)
2260 {
2261 	if (!skb) {
2262 		cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
2263 		return WORK_DONE_DESTROY;
2264 	}
2265 
2266 	if (wk->type == IEEE80211_WORK_AUTH) {
2267 		cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
2268 		return WORK_DONE_DESTROY;
2269 	}
2270 
2271 	mutex_lock(&wk->sdata->u.mgd.mtx);
2272 	ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
2273 	mutex_unlock(&wk->sdata->u.mgd.mtx);
2274 
2275 	wk->type = IEEE80211_WORK_AUTH;
2276 	wk->probe_auth.tries = 0;
2277 	return WORK_DONE_REQUEUE;
2278 }
2279 
ieee80211_mgd_auth(struct ieee80211_sub_if_data * sdata,struct cfg80211_auth_request * req)2280 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2281 		       struct cfg80211_auth_request *req)
2282 {
2283 	const u8 *ssid;
2284 	struct ieee80211_work *wk;
2285 	u16 auth_alg;
2286 
2287 	if (req->local_state_change)
2288 		return 0; /* no need to update mac80211 state */
2289 
2290 	switch (req->auth_type) {
2291 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
2292 		auth_alg = WLAN_AUTH_OPEN;
2293 		break;
2294 	case NL80211_AUTHTYPE_SHARED_KEY:
2295 		if (IS_ERR(sdata->local->wep_tx_tfm))
2296 			return -EOPNOTSUPP;
2297 		auth_alg = WLAN_AUTH_SHARED_KEY;
2298 		break;
2299 	case NL80211_AUTHTYPE_FT:
2300 		auth_alg = WLAN_AUTH_FT;
2301 		break;
2302 	case NL80211_AUTHTYPE_NETWORK_EAP:
2303 		auth_alg = WLAN_AUTH_LEAP;
2304 		break;
2305 	default:
2306 		return -EOPNOTSUPP;
2307 	}
2308 
2309 	wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2310 	if (!wk)
2311 		return -ENOMEM;
2312 
2313 	memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
2314 
2315 	if (req->ie && req->ie_len) {
2316 		memcpy(wk->ie, req->ie, req->ie_len);
2317 		wk->ie_len = req->ie_len;
2318 	}
2319 
2320 	if (req->key && req->key_len) {
2321 		wk->probe_auth.key_len = req->key_len;
2322 		wk->probe_auth.key_idx = req->key_idx;
2323 		memcpy(wk->probe_auth.key, req->key, req->key_len);
2324 	}
2325 
2326 	ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2327 	memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
2328 	wk->probe_auth.ssid_len = ssid[1];
2329 
2330 	wk->probe_auth.algorithm = auth_alg;
2331 	wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
2332 
2333 	/* if we already have a probe, don't probe again */
2334 	if (req->bss->proberesp_ies)
2335 		wk->type = IEEE80211_WORK_AUTH;
2336 	else
2337 		wk->type = IEEE80211_WORK_DIRECT_PROBE;
2338 	wk->chan = req->bss->channel;
2339 	wk->chan_type = NL80211_CHAN_NO_HT;
2340 	wk->sdata = sdata;
2341 	wk->done = ieee80211_probe_auth_done;
2342 
2343 	ieee80211_add_work(wk);
2344 	return 0;
2345 }
2346 
ieee80211_assoc_done(struct ieee80211_work * wk,struct sk_buff * skb)2347 static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
2348 						  struct sk_buff *skb)
2349 {
2350 	struct ieee80211_mgmt *mgmt;
2351 	struct ieee80211_rx_status *rx_status;
2352 	struct ieee802_11_elems elems;
2353 	u16 status;
2354 
2355 	if (!skb) {
2356 		cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
2357 		return WORK_DONE_DESTROY;
2358 	}
2359 
2360 	if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
2361 		mutex_lock(&wk->sdata->u.mgd.mtx);
2362 		rx_status = (void *) skb->cb;
2363 		ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
2364 		ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
2365 				      &elems, true);
2366 		mutex_unlock(&wk->sdata->u.mgd.mtx);
2367 
2368 		wk->type = IEEE80211_WORK_ASSOC;
2369 		/* not really done yet */
2370 		return WORK_DONE_REQUEUE;
2371 	}
2372 
2373 	mgmt = (void *)skb->data;
2374 	status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2375 
2376 	if (status == WLAN_STATUS_SUCCESS) {
2377 		mutex_lock(&wk->sdata->u.mgd.mtx);
2378 		if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
2379 			mutex_unlock(&wk->sdata->u.mgd.mtx);
2380 			/* oops -- internal error -- send timeout for now */
2381 			cfg80211_send_assoc_timeout(wk->sdata->dev,
2382 						    wk->filter_ta);
2383 			return WORK_DONE_DESTROY;
2384 		}
2385 
2386 		mutex_unlock(&wk->sdata->u.mgd.mtx);
2387 	}
2388 
2389 	cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
2390 	return WORK_DONE_DESTROY;
2391 }
2392 
ieee80211_mgd_assoc(struct ieee80211_sub_if_data * sdata,struct cfg80211_assoc_request * req)2393 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2394 			struct cfg80211_assoc_request *req)
2395 {
2396 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2397 	struct ieee80211_bss *bss = (void *)req->bss->priv;
2398 	struct ieee80211_work *wk;
2399 	const u8 *ssid;
2400 	int i;
2401 
2402 	mutex_lock(&ifmgd->mtx);
2403 	if (ifmgd->associated) {
2404 		if (!req->prev_bssid ||
2405 		    memcmp(req->prev_bssid, ifmgd->associated->bssid,
2406 			   ETH_ALEN)) {
2407 			/*
2408 			 * We are already associated and the request was not a
2409 			 * reassociation request from the current BSS, so
2410 			 * reject it.
2411 			 */
2412 			mutex_unlock(&ifmgd->mtx);
2413 			return -EALREADY;
2414 		}
2415 
2416 		/* Trying to reassociate - clear previous association state */
2417 		ieee80211_set_disassoc(sdata, true, false);
2418 	}
2419 	mutex_unlock(&ifmgd->mtx);
2420 
2421 	wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2422 	if (!wk)
2423 		return -ENOMEM;
2424 
2425 	ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
2426 	ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
2427 
2428 	ifmgd->beacon_crc_valid = false;
2429 
2430 	for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2431 		if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2432 		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2433 		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2434 			ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2435 
2436 
2437 	if (req->ie && req->ie_len) {
2438 		memcpy(wk->ie, req->ie, req->ie_len);
2439 		wk->ie_len = req->ie_len;
2440 	} else
2441 		wk->ie_len = 0;
2442 
2443 	wk->assoc.bss = req->bss;
2444 
2445 	memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
2446 
2447 	/* new association always uses requested smps mode */
2448 	if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
2449 		if (ifmgd->powersave)
2450 			ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
2451 		else
2452 			ifmgd->ap_smps = IEEE80211_SMPS_OFF;
2453 	} else
2454 		ifmgd->ap_smps = ifmgd->req_smps;
2455 
2456 	wk->assoc.smps = ifmgd->ap_smps;
2457 	/*
2458 	 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
2459 	 * We still associate in non-HT mode (11a/b/g) if any one of these
2460 	 * ciphers is configured as pairwise.
2461 	 * We can set this to true for non-11n hardware, that'll be checked
2462 	 * separately along with the peer capabilities.
2463 	 */
2464 	wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
2465 	wk->assoc.capability = req->bss->capability;
2466 	wk->assoc.wmm_used = bss->wmm_used;
2467 	wk->assoc.supp_rates = bss->supp_rates;
2468 	wk->assoc.supp_rates_len = bss->supp_rates_len;
2469 	wk->assoc.ht_information_ie =
2470 		ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
2471 
2472 	if (bss->wmm_used && bss->uapsd_supported &&
2473 	    (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
2474 		wk->assoc.uapsd_used = true;
2475 		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
2476 	} else {
2477 		wk->assoc.uapsd_used = false;
2478 		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
2479 	}
2480 
2481 	ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2482 	memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
2483 	wk->assoc.ssid_len = ssid[1];
2484 
2485 	if (req->prev_bssid)
2486 		memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
2487 
2488 	wk->chan = req->bss->channel;
2489 	wk->chan_type = NL80211_CHAN_NO_HT;
2490 	wk->sdata = sdata;
2491 	wk->done = ieee80211_assoc_done;
2492 	if (!bss->dtim_period &&
2493 	    sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
2494 		wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
2495 	else
2496 		wk->type = IEEE80211_WORK_ASSOC;
2497 
2498 	if (req->use_mfp) {
2499 		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2500 		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2501 	} else {
2502 		ifmgd->mfp = IEEE80211_MFP_DISABLED;
2503 		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2504 	}
2505 
2506 	if (req->crypto.control_port)
2507 		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2508 	else
2509 		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2510 
2511 	sdata->control_port_protocol = req->crypto.control_port_ethertype;
2512 	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
2513 
2514 	ieee80211_add_work(wk);
2515 	return 0;
2516 }
2517 
ieee80211_mgd_deauth(struct ieee80211_sub_if_data * sdata,struct cfg80211_deauth_request * req,void * cookie)2518 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
2519 			 struct cfg80211_deauth_request *req,
2520 			 void *cookie)
2521 {
2522 	struct ieee80211_local *local = sdata->local;
2523 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2524 	struct ieee80211_work *wk;
2525 	u8 bssid[ETH_ALEN];
2526 	bool assoc_bss = false;
2527 
2528 	mutex_lock(&ifmgd->mtx);
2529 
2530 	memcpy(bssid, req->bss->bssid, ETH_ALEN);
2531 	if (ifmgd->associated == req->bss) {
2532 		ieee80211_set_disassoc(sdata, false, true);
2533 		mutex_unlock(&ifmgd->mtx);
2534 		assoc_bss = true;
2535 	} else {
2536 		bool not_auth_yet = false;
2537 
2538 		mutex_unlock(&ifmgd->mtx);
2539 
2540 		mutex_lock(&local->mtx);
2541 		list_for_each_entry(wk, &local->work_list, list) {
2542 			if (wk->sdata != sdata)
2543 				continue;
2544 
2545 			if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
2546 			    wk->type != IEEE80211_WORK_AUTH &&
2547 			    wk->type != IEEE80211_WORK_ASSOC &&
2548 			    wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
2549 				continue;
2550 
2551 			if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
2552 				continue;
2553 
2554 			not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
2555 			list_del_rcu(&wk->list);
2556 			free_work(wk);
2557 			break;
2558 		}
2559 		mutex_unlock(&local->mtx);
2560 
2561 		/*
2562 		 * If somebody requests authentication and we haven't
2563 		 * sent out an auth frame yet there's no need to send
2564 		 * out a deauth frame either. If the state was PROBE,
2565 		 * then this is the case. If it's AUTH we have sent a
2566 		 * frame, and if it's IDLE we have completed the auth
2567 		 * process already.
2568 		 */
2569 		if (not_auth_yet) {
2570 			__cfg80211_auth_canceled(sdata->dev, bssid);
2571 			return 0;
2572 		}
2573 	}
2574 
2575 	printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
2576 	       sdata->name, bssid, req->reason_code);
2577 
2578 	ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
2579 				       req->reason_code, cookie,
2580 				       !req->local_state_change);
2581 	if (assoc_bss)
2582 		sta_info_destroy_addr(sdata, bssid);
2583 
2584 	mutex_lock(&sdata->local->mtx);
2585 	ieee80211_recalc_idle(sdata->local);
2586 	mutex_unlock(&sdata->local->mtx);
2587 
2588 	return 0;
2589 }
2590 
ieee80211_mgd_disassoc(struct ieee80211_sub_if_data * sdata,struct cfg80211_disassoc_request * req,void * cookie)2591 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
2592 			   struct cfg80211_disassoc_request *req,
2593 			   void *cookie)
2594 {
2595 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2596 	u8 bssid[ETH_ALEN];
2597 
2598 	mutex_lock(&ifmgd->mtx);
2599 
2600 	/*
2601 	 * cfg80211 should catch this ... but it's racy since
2602 	 * we can receive a disassoc frame, process it, hand it
2603 	 * to cfg80211 while that's in a locked section already
2604 	 * trying to tell us that the user wants to disconnect.
2605 	 */
2606 	if (ifmgd->associated != req->bss) {
2607 		mutex_unlock(&ifmgd->mtx);
2608 		return -ENOLINK;
2609 	}
2610 
2611 	printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
2612 	       sdata->name, req->bss->bssid, req->reason_code);
2613 
2614 	memcpy(bssid, req->bss->bssid, ETH_ALEN);
2615 	ieee80211_set_disassoc(sdata, false, true);
2616 
2617 	mutex_unlock(&ifmgd->mtx);
2618 
2619 	ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
2620 			IEEE80211_STYPE_DISASSOC, req->reason_code,
2621 			cookie, !req->local_state_change);
2622 	sta_info_destroy_addr(sdata, bssid);
2623 
2624 	mutex_lock(&sdata->local->mtx);
2625 	ieee80211_recalc_idle(sdata->local);
2626 	mutex_unlock(&sdata->local->mtx);
2627 
2628 	return 0;
2629 }
2630 
ieee80211_cqm_rssi_notify(struct ieee80211_vif * vif,enum nl80211_cqm_rssi_threshold_event rssi_event,gfp_t gfp)2631 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
2632 			       enum nl80211_cqm_rssi_threshold_event rssi_event,
2633 			       gfp_t gfp)
2634 {
2635 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2636 
2637 	trace_api_cqm_rssi_notify(sdata, rssi_event);
2638 
2639 	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
2640 }
2641 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
2642