1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/timer.h>
19 #include <linux/rtnetlink.h>
20 
21 #include <net/mac80211.h>
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
24 #include "rate.h"
25 #include "sta_info.h"
26 #include "debugfs_sta.h"
27 #include "mesh.h"
28 #include "wme.h"
29 
30 /**
31  * DOC: STA information lifetime rules
32  *
33  * STA info structures (&struct sta_info) are managed in a hash table
34  * for faster lookup and a list for iteration. They are managed using
35  * RCU, i.e. access to the list and hash table is protected by RCU.
36  *
37  * Upon allocating a STA info structure with sta_info_alloc(), the caller
38  * owns that structure. It must then insert it into the hash table using
39  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
40  * case (which acquires an rcu read section but must not be called from
41  * within one) will the pointer still be valid after the call. Note that
42  * the caller may not do much with the STA info before inserting it, in
43  * particular, it may not start any mesh peer link management or add
44  * encryption keys.
45  *
46  * When the insertion fails (sta_info_insert()) returns non-zero), the
47  * structure will have been freed by sta_info_insert()!
48  *
49  * Station entries are added by mac80211 when you establish a link with a
50  * peer. This means different things for the different type of interfaces
51  * we support. For a regular station this mean we add the AP sta when we
52  * receive an association response from the AP. For IBSS this occurs when
53  * get to know about a peer on the same IBSS. For WDS we add the sta for
54  * the peer immediately upon device open. When using AP mode we add stations
55  * for each respective station upon request from userspace through nl80211.
56  *
57  * In order to remove a STA info structure, various sta_info_destroy_*()
58  * calls are available.
59  *
60  * There is no concept of ownership on a STA entry, each structure is
61  * owned by the global hash table/list until it is removed. All users of
62  * the structure need to be RCU protected so that the structure won't be
63  * freed before they are done using it.
64  */
65 
66 /* Caller must hold local->sta_mtx */
sta_info_hash_del(struct ieee80211_local * local,struct sta_info * sta)67 static int sta_info_hash_del(struct ieee80211_local *local,
68 			     struct sta_info *sta)
69 {
70 	struct sta_info *s;
71 
72 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
73 				      lockdep_is_held(&local->sta_mtx));
74 	if (!s)
75 		return -ENOENT;
76 	if (s == sta) {
77 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
78 				   s->hnext);
79 		return 0;
80 	}
81 
82 	while (rcu_access_pointer(s->hnext) &&
83 	       rcu_access_pointer(s->hnext) != sta)
84 		s = rcu_dereference_protected(s->hnext,
85 					lockdep_is_held(&local->sta_mtx));
86 	if (rcu_access_pointer(s->hnext)) {
87 		rcu_assign_pointer(s->hnext, sta->hnext);
88 		return 0;
89 	}
90 
91 	return -ENOENT;
92 }
93 
94 /* protected by RCU */
sta_info_get(struct ieee80211_sub_if_data * sdata,const u8 * addr)95 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
96 			      const u8 *addr)
97 {
98 	struct ieee80211_local *local = sdata->local;
99 	struct sta_info *sta;
100 
101 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
102 				    lockdep_is_held(&local->sta_mtx));
103 	while (sta) {
104 		if (sta->sdata == sdata &&
105 		    compare_ether_addr(sta->sta.addr, addr) == 0)
106 			break;
107 		sta = rcu_dereference_check(sta->hnext,
108 					    lockdep_is_held(&local->sta_mtx));
109 	}
110 	return sta;
111 }
112 
113 /*
114  * Get sta info either from the specified interface
115  * or from one of its vlans
116  */
sta_info_get_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)117 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
118 				  const u8 *addr)
119 {
120 	struct ieee80211_local *local = sdata->local;
121 	struct sta_info *sta;
122 
123 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
124 				    lockdep_is_held(&local->sta_mtx));
125 	while (sta) {
126 		if ((sta->sdata == sdata ||
127 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
128 		    compare_ether_addr(sta->sta.addr, addr) == 0)
129 			break;
130 		sta = rcu_dereference_check(sta->hnext,
131 					    lockdep_is_held(&local->sta_mtx));
132 	}
133 	return sta;
134 }
135 
sta_info_get_by_idx(struct ieee80211_sub_if_data * sdata,int idx)136 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
137 				     int idx)
138 {
139 	struct ieee80211_local *local = sdata->local;
140 	struct sta_info *sta;
141 	int i = 0;
142 
143 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
144 		if (sdata != sta->sdata)
145 			continue;
146 		if (i < idx) {
147 			++i;
148 			continue;
149 		}
150 		return sta;
151 	}
152 
153 	return NULL;
154 }
155 
156 /**
157  * sta_info_free - free STA
158  *
159  * @local: pointer to the global information
160  * @sta: STA info to free
161  *
162  * This function must undo everything done by sta_info_alloc()
163  * that may happen before sta_info_insert(). It may only be
164  * called when sta_info_insert() has not been attempted (and
165  * if that fails, the station is freed anyway.)
166  */
sta_info_free(struct ieee80211_local * local,struct sta_info * sta)167 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
168 {
169 	if (sta->rate_ctrl)
170 		rate_control_free_sta(sta);
171 
172 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
173 	wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
174 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
175 
176 	kfree(sta);
177 }
178 
179 /* Caller must hold local->sta_mtx */
sta_info_hash_add(struct ieee80211_local * local,struct sta_info * sta)180 static void sta_info_hash_add(struct ieee80211_local *local,
181 			      struct sta_info *sta)
182 {
183 	lockdep_assert_held(&local->sta_mtx);
184 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
185 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
186 }
187 
sta_unblock(struct work_struct * wk)188 static void sta_unblock(struct work_struct *wk)
189 {
190 	struct sta_info *sta;
191 
192 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
193 
194 	if (sta->dead)
195 		return;
196 
197 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
198 		local_bh_disable();
199 		ieee80211_sta_ps_deliver_wakeup(sta);
200 		local_bh_enable();
201 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
202 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
203 
204 		local_bh_disable();
205 		ieee80211_sta_ps_deliver_poll_response(sta);
206 		local_bh_enable();
207 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
208 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
209 
210 		local_bh_disable();
211 		ieee80211_sta_ps_deliver_uapsd(sta);
212 		local_bh_enable();
213 	} else
214 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
215 }
216 
sta_prepare_rate_control(struct ieee80211_local * local,struct sta_info * sta,gfp_t gfp)217 static int sta_prepare_rate_control(struct ieee80211_local *local,
218 				    struct sta_info *sta, gfp_t gfp)
219 {
220 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
221 		return 0;
222 
223 	sta->rate_ctrl = local->rate_ctrl;
224 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
225 						     &sta->sta, gfp);
226 	if (!sta->rate_ctrl_priv)
227 		return -ENOMEM;
228 
229 	return 0;
230 }
231 
sta_info_alloc(struct ieee80211_sub_if_data * sdata,const u8 * addr,gfp_t gfp)232 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
233 				const u8 *addr, gfp_t gfp)
234 {
235 	struct ieee80211_local *local = sdata->local;
236 	struct sta_info *sta;
237 	struct timespec uptime;
238 	int i;
239 
240 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
241 	if (!sta)
242 		return NULL;
243 
244 	spin_lock_init(&sta->lock);
245 	spin_lock_init(&sta->ps_lock);
246 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
247 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
248 	mutex_init(&sta->ampdu_mlme.mtx);
249 
250 	memcpy(sta->sta.addr, addr, ETH_ALEN);
251 	sta->local = local;
252 	sta->sdata = sdata;
253 	sta->last_rx = jiffies;
254 
255 	sta->sta_state = IEEE80211_STA_NONE;
256 
257 	do_posix_clock_monotonic_gettime(&uptime);
258 	sta->last_connected = uptime.tv_sec;
259 	ewma_init(&sta->avg_signal, 1024, 8);
260 
261 	if (sta_prepare_rate_control(local, sta, gfp)) {
262 		kfree(sta);
263 		return NULL;
264 	}
265 
266 	for (i = 0; i < STA_TID_NUM; i++) {
267 		/*
268 		 * timer_to_tid must be initialized with identity mapping
269 		 * to enable session_timer's data differentiation. See
270 		 * sta_rx_agg_session_timer_expired for usage.
271 		 */
272 		sta->timer_to_tid[i] = i;
273 	}
274 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
275 		skb_queue_head_init(&sta->ps_tx_buf[i]);
276 		skb_queue_head_init(&sta->tx_filtered[i]);
277 	}
278 
279 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
280 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
281 
282 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
283 	wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
284 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
285 
286 #ifdef CONFIG_MAC80211_MESH
287 	sta->plink_state = NL80211_PLINK_LISTEN;
288 	init_timer(&sta->plink_timer);
289 #endif
290 
291 	return sta;
292 }
293 
sta_info_insert_check(struct sta_info * sta)294 static int sta_info_insert_check(struct sta_info *sta)
295 {
296 	struct ieee80211_sub_if_data *sdata = sta->sdata;
297 
298 	/*
299 	 * Can't be a WARN_ON because it can be triggered through a race:
300 	 * something inserts a STA (on one CPU) without holding the RTNL
301 	 * and another CPU turns off the net device.
302 	 */
303 	if (unlikely(!ieee80211_sdata_running(sdata)))
304 		return -ENETDOWN;
305 
306 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
307 		    is_multicast_ether_addr(sta->sta.addr)))
308 		return -EINVAL;
309 
310 	return 0;
311 }
312 
sta_info_insert_drv_state(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct sta_info * sta)313 static int sta_info_insert_drv_state(struct ieee80211_local *local,
314 				     struct ieee80211_sub_if_data *sdata,
315 				     struct sta_info *sta)
316 {
317 	enum ieee80211_sta_state state;
318 	int err = 0;
319 
320 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
321 		err = drv_sta_state(local, sdata, sta, state, state + 1);
322 		if (err)
323 			break;
324 	}
325 
326 	if (!err) {
327 		/*
328 		 * Drivers using legacy sta_add/sta_remove callbacks only
329 		 * get uploaded set to true after sta_add is called.
330 		 */
331 		if (!local->ops->sta_add)
332 			sta->uploaded = true;
333 		return 0;
334 	}
335 
336 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
337 		printk(KERN_DEBUG
338 		       "%s: failed to move IBSS STA %pM to state %d (%d) - keeping it anyway.\n",
339 		       sdata->name, sta->sta.addr, state + 1, err);
340 		err = 0;
341 	}
342 
343 	/* unwind on error */
344 	for (; state > IEEE80211_STA_NOTEXIST; state--)
345 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
346 
347 	return err;
348 }
349 
350 /*
351  * should be called with sta_mtx locked
352  * this function replaces the mutex lock
353  * with a RCU lock
354  */
sta_info_insert_finish(struct sta_info * sta)355 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
356 {
357 	struct ieee80211_local *local = sta->local;
358 	struct ieee80211_sub_if_data *sdata = sta->sdata;
359 	struct station_info sinfo;
360 	int err = 0;
361 
362 	lockdep_assert_held(&local->sta_mtx);
363 
364 	/* check if STA exists already */
365 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
366 		err = -EEXIST;
367 		goto out_err;
368 	}
369 
370 	/* notify driver */
371 	err = sta_info_insert_drv_state(local, sdata, sta);
372 	if (err)
373 		goto out_err;
374 
375 	local->num_sta++;
376 	local->sta_generation++;
377 	smp_mb();
378 
379 	/* make the station visible */
380 	sta_info_hash_add(local, sta);
381 
382 	list_add_rcu(&sta->list, &local->sta_list);
383 
384 	set_sta_flag(sta, WLAN_STA_INSERTED);
385 
386 	ieee80211_sta_debugfs_add(sta);
387 	rate_control_add_sta_debugfs(sta);
388 
389 	memset(&sinfo, 0, sizeof(sinfo));
390 	sinfo.filled = 0;
391 	sinfo.generation = local->sta_generation;
392 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
393 
394 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
395 	wiphy_debug(local->hw.wiphy, "Inserted STA %pM\n", sta->sta.addr);
396 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
397 
398 	/* move reference to rcu-protected */
399 	rcu_read_lock();
400 	mutex_unlock(&local->sta_mtx);
401 
402 	if (ieee80211_vif_is_mesh(&sdata->vif))
403 		mesh_accept_plinks_update(sdata);
404 
405 	return 0;
406  out_err:
407 	mutex_unlock(&local->sta_mtx);
408 	rcu_read_lock();
409 	return err;
410 }
411 
sta_info_insert_rcu(struct sta_info * sta)412 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
413 {
414 	struct ieee80211_local *local = sta->local;
415 	int err = 0;
416 
417 	might_sleep();
418 
419 	err = sta_info_insert_check(sta);
420 	if (err) {
421 		rcu_read_lock();
422 		goto out_free;
423 	}
424 
425 	mutex_lock(&local->sta_mtx);
426 
427 	err = sta_info_insert_finish(sta);
428 	if (err)
429 		goto out_free;
430 
431 	return 0;
432  out_free:
433 	BUG_ON(!err);
434 	sta_info_free(local, sta);
435 	return err;
436 }
437 
sta_info_insert(struct sta_info * sta)438 int sta_info_insert(struct sta_info *sta)
439 {
440 	int err = sta_info_insert_rcu(sta);
441 
442 	rcu_read_unlock();
443 
444 	return err;
445 }
446 
__bss_tim_set(struct ieee80211_if_ap * bss,u16 aid)447 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
448 {
449 	/*
450 	 * This format has been mandated by the IEEE specifications,
451 	 * so this line may not be changed to use the __set_bit() format.
452 	 */
453 	bss->tim[aid / 8] |= (1 << (aid % 8));
454 }
455 
__bss_tim_clear(struct ieee80211_if_ap * bss,u16 aid)456 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
457 {
458 	/*
459 	 * This format has been mandated by the IEEE specifications,
460 	 * so this line may not be changed to use the __clear_bit() format.
461 	 */
462 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
463 }
464 
ieee80211_tids_for_ac(int ac)465 static unsigned long ieee80211_tids_for_ac(int ac)
466 {
467 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
468 	switch (ac) {
469 	case IEEE80211_AC_VO:
470 		return BIT(6) | BIT(7);
471 	case IEEE80211_AC_VI:
472 		return BIT(4) | BIT(5);
473 	case IEEE80211_AC_BE:
474 		return BIT(0) | BIT(3);
475 	case IEEE80211_AC_BK:
476 		return BIT(1) | BIT(2);
477 	default:
478 		WARN_ON(1);
479 		return 0;
480 	}
481 }
482 
sta_info_recalc_tim(struct sta_info * sta)483 void sta_info_recalc_tim(struct sta_info *sta)
484 {
485 	struct ieee80211_local *local = sta->local;
486 	struct ieee80211_if_ap *bss = sta->sdata->bss;
487 	unsigned long flags;
488 	bool indicate_tim = false;
489 	u8 ignore_for_tim = sta->sta.uapsd_queues;
490 	int ac;
491 
492 	if (WARN_ON_ONCE(!sta->sdata->bss))
493 		return;
494 
495 	/* No need to do anything if the driver does all */
496 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
497 		return;
498 
499 	if (sta->dead)
500 		goto done;
501 
502 	/*
503 	 * If all ACs are delivery-enabled then we should build
504 	 * the TIM bit for all ACs anyway; if only some are then
505 	 * we ignore those and build the TIM bit using only the
506 	 * non-enabled ones.
507 	 */
508 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
509 		ignore_for_tim = 0;
510 
511 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
512 		unsigned long tids;
513 
514 		if (ignore_for_tim & BIT(ac))
515 			continue;
516 
517 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
518 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
519 		if (indicate_tim)
520 			break;
521 
522 		tids = ieee80211_tids_for_ac(ac);
523 
524 		indicate_tim |=
525 			sta->driver_buffered_tids & tids;
526 	}
527 
528  done:
529 	spin_lock_irqsave(&local->tim_lock, flags);
530 
531 	if (indicate_tim)
532 		__bss_tim_set(bss, sta->sta.aid);
533 	else
534 		__bss_tim_clear(bss, sta->sta.aid);
535 
536 	if (local->ops->set_tim) {
537 		local->tim_in_locked_section = true;
538 		drv_set_tim(local, &sta->sta, indicate_tim);
539 		local->tim_in_locked_section = false;
540 	}
541 
542 	spin_unlock_irqrestore(&local->tim_lock, flags);
543 }
544 
sta_info_buffer_expired(struct sta_info * sta,struct sk_buff * skb)545 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
546 {
547 	struct ieee80211_tx_info *info;
548 	int timeout;
549 
550 	if (!skb)
551 		return false;
552 
553 	info = IEEE80211_SKB_CB(skb);
554 
555 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
556 	timeout = (sta->listen_interval *
557 		   sta->sdata->vif.bss_conf.beacon_int *
558 		   32 / 15625) * HZ;
559 	if (timeout < STA_TX_BUFFER_EXPIRE)
560 		timeout = STA_TX_BUFFER_EXPIRE;
561 	return time_after(jiffies, info->control.jiffies + timeout);
562 }
563 
564 
sta_info_cleanup_expire_buffered_ac(struct ieee80211_local * local,struct sta_info * sta,int ac)565 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
566 						struct sta_info *sta, int ac)
567 {
568 	unsigned long flags;
569 	struct sk_buff *skb;
570 
571 	/*
572 	 * First check for frames that should expire on the filtered
573 	 * queue. Frames here were rejected by the driver and are on
574 	 * a separate queue to avoid reordering with normal PS-buffered
575 	 * frames. They also aren't accounted for right now in the
576 	 * total_ps_buffered counter.
577 	 */
578 	for (;;) {
579 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
580 		skb = skb_peek(&sta->tx_filtered[ac]);
581 		if (sta_info_buffer_expired(sta, skb))
582 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
583 		else
584 			skb = NULL;
585 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
586 
587 		/*
588 		 * Frames are queued in order, so if this one
589 		 * hasn't expired yet we can stop testing. If
590 		 * we actually reached the end of the queue we
591 		 * also need to stop, of course.
592 		 */
593 		if (!skb)
594 			break;
595 		dev_kfree_skb(skb);
596 	}
597 
598 	/*
599 	 * Now also check the normal PS-buffered queue, this will
600 	 * only find something if the filtered queue was emptied
601 	 * since the filtered frames are all before the normal PS
602 	 * buffered frames.
603 	 */
604 	for (;;) {
605 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
606 		skb = skb_peek(&sta->ps_tx_buf[ac]);
607 		if (sta_info_buffer_expired(sta, skb))
608 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
609 		else
610 			skb = NULL;
611 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
612 
613 		/*
614 		 * frames are queued in order, so if this one
615 		 * hasn't expired yet (or we reached the end of
616 		 * the queue) we can stop testing
617 		 */
618 		if (!skb)
619 			break;
620 
621 		local->total_ps_buffered--;
622 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
623 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
624 		       sta->sta.addr);
625 #endif
626 		dev_kfree_skb(skb);
627 	}
628 
629 	/*
630 	 * Finally, recalculate the TIM bit for this station -- it might
631 	 * now be clear because the station was too slow to retrieve its
632 	 * frames.
633 	 */
634 	sta_info_recalc_tim(sta);
635 
636 	/*
637 	 * Return whether there are any frames still buffered, this is
638 	 * used to check whether the cleanup timer still needs to run,
639 	 * if there are no frames we don't need to rearm the timer.
640 	 */
641 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
642 		 skb_queue_empty(&sta->tx_filtered[ac]));
643 }
644 
sta_info_cleanup_expire_buffered(struct ieee80211_local * local,struct sta_info * sta)645 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
646 					     struct sta_info *sta)
647 {
648 	bool have_buffered = false;
649 	int ac;
650 
651 	/* This is only necessary for stations on BSS interfaces */
652 	if (!sta->sdata->bss)
653 		return false;
654 
655 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
656 		have_buffered |=
657 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
658 
659 	return have_buffered;
660 }
661 
__sta_info_destroy(struct sta_info * sta)662 int __must_check __sta_info_destroy(struct sta_info *sta)
663 {
664 	struct ieee80211_local *local;
665 	struct ieee80211_sub_if_data *sdata;
666 	int ret, i, ac;
667 	struct tid_ampdu_tx *tid_tx;
668 
669 	might_sleep();
670 
671 	if (!sta)
672 		return -ENOENT;
673 
674 	local = sta->local;
675 	sdata = sta->sdata;
676 
677 	lockdep_assert_held(&local->sta_mtx);
678 
679 	/*
680 	 * Before removing the station from the driver and
681 	 * rate control, it might still start new aggregation
682 	 * sessions -- block that to make sure the tear-down
683 	 * will be sufficient.
684 	 */
685 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
686 	ieee80211_sta_tear_down_BA_sessions(sta, true);
687 
688 	ret = sta_info_hash_del(local, sta);
689 	if (ret)
690 		return ret;
691 
692 	list_del_rcu(&sta->list);
693 
694 	mutex_lock(&local->key_mtx);
695 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
696 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
697 	if (sta->ptk)
698 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
699 	mutex_unlock(&local->key_mtx);
700 
701 	sta->dead = true;
702 
703 	local->num_sta--;
704 	local->sta_generation++;
705 
706 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
707 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
708 
709 	while (sta->sta_state > IEEE80211_STA_NONE) {
710 		ret = sta_info_move_state(sta, sta->sta_state - 1);
711 		if (ret) {
712 			WARN_ON_ONCE(1);
713 			break;
714 		}
715 	}
716 
717 	if (sta->uploaded) {
718 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
719 				    IEEE80211_STA_NOTEXIST);
720 		WARN_ON_ONCE(ret != 0);
721 	}
722 
723 	/*
724 	 * At this point, after we wait for an RCU grace period,
725 	 * neither mac80211 nor the driver can reference this
726 	 * sta struct any more except by still existing timers
727 	 * associated with this station that we clean up below.
728 	 */
729 	synchronize_rcu();
730 
731 	if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
732 		BUG_ON(!sdata->bss);
733 
734 		clear_sta_flag(sta, WLAN_STA_PS_STA);
735 
736 		atomic_dec(&sdata->bss->num_sta_ps);
737 		sta_info_recalc_tim(sta);
738 	}
739 
740 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
741 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
742 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
743 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
744 	}
745 
746 #ifdef CONFIG_MAC80211_MESH
747 	if (ieee80211_vif_is_mesh(&sdata->vif))
748 		mesh_accept_plinks_update(sdata);
749 #endif
750 
751 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
752 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
753 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
754 	cancel_work_sync(&sta->drv_unblock_wk);
755 
756 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
757 
758 	rate_control_remove_sta_debugfs(sta);
759 	ieee80211_sta_debugfs_remove(sta);
760 
761 #ifdef CONFIG_MAC80211_MESH
762 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
763 		mesh_plink_deactivate(sta);
764 		del_timer_sync(&sta->plink_timer);
765 	}
766 #endif
767 
768 	/*
769 	 * Destroy aggregation state here. It would be nice to wait for the
770 	 * driver to finish aggregation stop and then clean up, but for now
771 	 * drivers have to handle aggregation stop being requested, followed
772 	 * directly by station destruction.
773 	 */
774 	for (i = 0; i < STA_TID_NUM; i++) {
775 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
776 		if (!tid_tx)
777 			continue;
778 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
779 		kfree(tid_tx);
780 	}
781 
782 	sta_info_free(local, sta);
783 
784 	return 0;
785 }
786 
sta_info_destroy_addr(struct ieee80211_sub_if_data * sdata,const u8 * addr)787 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
788 {
789 	struct sta_info *sta;
790 	int ret;
791 
792 	mutex_lock(&sdata->local->sta_mtx);
793 	sta = sta_info_get(sdata, addr);
794 	ret = __sta_info_destroy(sta);
795 	mutex_unlock(&sdata->local->sta_mtx);
796 
797 	return ret;
798 }
799 
sta_info_destroy_addr_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)800 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
801 			      const u8 *addr)
802 {
803 	struct sta_info *sta;
804 	int ret;
805 
806 	mutex_lock(&sdata->local->sta_mtx);
807 	sta = sta_info_get_bss(sdata, addr);
808 	ret = __sta_info_destroy(sta);
809 	mutex_unlock(&sdata->local->sta_mtx);
810 
811 	return ret;
812 }
813 
sta_info_cleanup(unsigned long data)814 static void sta_info_cleanup(unsigned long data)
815 {
816 	struct ieee80211_local *local = (struct ieee80211_local *) data;
817 	struct sta_info *sta;
818 	bool timer_needed = false;
819 
820 	rcu_read_lock();
821 	list_for_each_entry_rcu(sta, &local->sta_list, list)
822 		if (sta_info_cleanup_expire_buffered(local, sta))
823 			timer_needed = true;
824 	rcu_read_unlock();
825 
826 	if (local->quiescing)
827 		return;
828 
829 	if (!timer_needed)
830 		return;
831 
832 	mod_timer(&local->sta_cleanup,
833 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
834 }
835 
sta_info_init(struct ieee80211_local * local)836 void sta_info_init(struct ieee80211_local *local)
837 {
838 	spin_lock_init(&local->tim_lock);
839 	mutex_init(&local->sta_mtx);
840 	INIT_LIST_HEAD(&local->sta_list);
841 
842 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
843 		    (unsigned long)local);
844 }
845 
sta_info_stop(struct ieee80211_local * local)846 void sta_info_stop(struct ieee80211_local *local)
847 {
848 	del_timer_sync(&local->sta_cleanup);
849 	sta_info_flush(local, NULL);
850 }
851 
852 /**
853  * sta_info_flush - flush matching STA entries from the STA table
854  *
855  * Returns the number of removed STA entries.
856  *
857  * @local: local interface data
858  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
859  */
sta_info_flush(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata)860 int sta_info_flush(struct ieee80211_local *local,
861 		   struct ieee80211_sub_if_data *sdata)
862 {
863 	struct sta_info *sta, *tmp;
864 	int ret = 0;
865 
866 	might_sleep();
867 
868 	mutex_lock(&local->sta_mtx);
869 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
870 		if (!sdata || sdata == sta->sdata) {
871 			WARN_ON(__sta_info_destroy(sta));
872 			ret++;
873 		}
874 	}
875 	mutex_unlock(&local->sta_mtx);
876 
877 	return ret;
878 }
879 
ieee80211_sta_expire(struct ieee80211_sub_if_data * sdata,unsigned long exp_time)880 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
881 			  unsigned long exp_time)
882 {
883 	struct ieee80211_local *local = sdata->local;
884 	struct sta_info *sta, *tmp;
885 
886 	mutex_lock(&local->sta_mtx);
887 
888 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
889 		if (sdata != sta->sdata)
890 			continue;
891 
892 		if (time_after(jiffies, sta->last_rx + exp_time)) {
893 #ifdef CONFIG_MAC80211_IBSS_DEBUG
894 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
895 			       sdata->name, sta->sta.addr);
896 #endif
897 			WARN_ON(__sta_info_destroy(sta));
898 		}
899 	}
900 
901 	mutex_unlock(&local->sta_mtx);
902 }
903 
ieee80211_find_sta_by_ifaddr(struct ieee80211_hw * hw,const u8 * addr,const u8 * localaddr)904 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
905 					       const u8 *addr,
906 					       const u8 *localaddr)
907 {
908 	struct sta_info *sta, *nxt;
909 
910 	/*
911 	 * Just return a random station if localaddr is NULL
912 	 * ... first in list.
913 	 */
914 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
915 		if (localaddr &&
916 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
917 			continue;
918 		if (!sta->uploaded)
919 			return NULL;
920 		return &sta->sta;
921 	}
922 
923 	return NULL;
924 }
925 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
926 
ieee80211_find_sta(struct ieee80211_vif * vif,const u8 * addr)927 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
928 					 const u8 *addr)
929 {
930 	struct sta_info *sta;
931 
932 	if (!vif)
933 		return NULL;
934 
935 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
936 	if (!sta)
937 		return NULL;
938 
939 	if (!sta->uploaded)
940 		return NULL;
941 
942 	return &sta->sta;
943 }
944 EXPORT_SYMBOL(ieee80211_find_sta);
945 
clear_sta_ps_flags(void * _sta)946 static void clear_sta_ps_flags(void *_sta)
947 {
948 	struct sta_info *sta = _sta;
949 	struct ieee80211_sub_if_data *sdata = sta->sdata;
950 
951 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
952 	if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
953 		atomic_dec(&sdata->bss->num_sta_ps);
954 }
955 
956 /* powersave support code */
ieee80211_sta_ps_deliver_wakeup(struct sta_info * sta)957 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
958 {
959 	struct ieee80211_sub_if_data *sdata = sta->sdata;
960 	struct ieee80211_local *local = sdata->local;
961 	struct sk_buff_head pending;
962 	int filtered = 0, buffered = 0, ac;
963 	unsigned long flags;
964 
965 	clear_sta_flag(sta, WLAN_STA_SP);
966 
967 	BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
968 	sta->driver_buffered_tids = 0;
969 
970 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
971 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
972 
973 	skb_queue_head_init(&pending);
974 
975 	/* sync with ieee80211_tx_h_unicast_ps_buf */
976 	spin_lock(&sta->ps_lock);
977 	/* Send all buffered frames to the station */
978 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
979 		int count = skb_queue_len(&pending), tmp;
980 
981 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
982 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
983 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
984 		tmp = skb_queue_len(&pending);
985 		filtered += tmp - count;
986 		count = tmp;
987 
988 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
989 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
990 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
991 		tmp = skb_queue_len(&pending);
992 		buffered += tmp - count;
993 	}
994 
995 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
996 	spin_unlock(&sta->ps_lock);
997 
998 	local->total_ps_buffered -= buffered;
999 
1000 	sta_info_recalc_tim(sta);
1001 
1002 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1003 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
1004 	       "since STA not sleeping anymore\n", sdata->name,
1005 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1006 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1007 }
1008 
ieee80211_send_null_response(struct ieee80211_sub_if_data * sdata,struct sta_info * sta,int tid,enum ieee80211_frame_release_type reason)1009 static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1010 					 struct sta_info *sta, int tid,
1011 					 enum ieee80211_frame_release_type reason)
1012 {
1013 	struct ieee80211_local *local = sdata->local;
1014 	struct ieee80211_qos_hdr *nullfunc;
1015 	struct sk_buff *skb;
1016 	int size = sizeof(*nullfunc);
1017 	__le16 fc;
1018 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1019 	struct ieee80211_tx_info *info;
1020 
1021 	if (qos) {
1022 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1023 				 IEEE80211_STYPE_QOS_NULLFUNC |
1024 				 IEEE80211_FCTL_FROMDS);
1025 	} else {
1026 		size -= 2;
1027 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1028 				 IEEE80211_STYPE_NULLFUNC |
1029 				 IEEE80211_FCTL_FROMDS);
1030 	}
1031 
1032 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1033 	if (!skb)
1034 		return;
1035 
1036 	skb_reserve(skb, local->hw.extra_tx_headroom);
1037 
1038 	nullfunc = (void *) skb_put(skb, size);
1039 	nullfunc->frame_control = fc;
1040 	nullfunc->duration_id = 0;
1041 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1042 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1043 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1044 
1045 	skb->priority = tid;
1046 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1047 	if (qos) {
1048 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1049 
1050 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1051 			nullfunc->qos_ctrl |=
1052 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1053 	}
1054 
1055 	info = IEEE80211_SKB_CB(skb);
1056 
1057 	/*
1058 	 * Tell TX path to send this frame even though the
1059 	 * STA may still remain is PS mode after this frame
1060 	 * exchange. Also set EOSP to indicate this packet
1061 	 * ends the poll/service period.
1062 	 */
1063 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1064 		       IEEE80211_TX_STATUS_EOSP |
1065 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1066 
1067 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
1068 
1069 	ieee80211_xmit(sdata, skb);
1070 }
1071 
1072 static void
ieee80211_sta_ps_deliver_response(struct sta_info * sta,int n_frames,u8 ignored_acs,enum ieee80211_frame_release_type reason)1073 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1074 				  int n_frames, u8 ignored_acs,
1075 				  enum ieee80211_frame_release_type reason)
1076 {
1077 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1078 	struct ieee80211_local *local = sdata->local;
1079 	bool found = false;
1080 	bool more_data = false;
1081 	int ac;
1082 	unsigned long driver_release_tids = 0;
1083 	struct sk_buff_head frames;
1084 
1085 	/* Service or PS-Poll period starts */
1086 	set_sta_flag(sta, WLAN_STA_SP);
1087 
1088 	__skb_queue_head_init(&frames);
1089 
1090 	/*
1091 	 * Get response frame(s) and more data bit for it.
1092 	 */
1093 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1094 		unsigned long tids;
1095 
1096 		if (ignored_acs & BIT(ac))
1097 			continue;
1098 
1099 		tids = ieee80211_tids_for_ac(ac);
1100 
1101 		if (!found) {
1102 			driver_release_tids = sta->driver_buffered_tids & tids;
1103 			if (driver_release_tids) {
1104 				found = true;
1105 			} else {
1106 				struct sk_buff *skb;
1107 
1108 				while (n_frames > 0) {
1109 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1110 					if (!skb) {
1111 						skb = skb_dequeue(
1112 							&sta->ps_tx_buf[ac]);
1113 						if (skb)
1114 							local->total_ps_buffered--;
1115 					}
1116 					if (!skb)
1117 						break;
1118 					n_frames--;
1119 					found = true;
1120 					__skb_queue_tail(&frames, skb);
1121 				}
1122 			}
1123 
1124 			/*
1125 			 * If the driver has data on more than one TID then
1126 			 * certainly there's more data if we release just a
1127 			 * single frame now (from a single TID).
1128 			 */
1129 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1130 			    hweight16(driver_release_tids) > 1) {
1131 				more_data = true;
1132 				driver_release_tids =
1133 					BIT(ffs(driver_release_tids) - 1);
1134 				break;
1135 			}
1136 		}
1137 
1138 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1139 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1140 			more_data = true;
1141 			break;
1142 		}
1143 	}
1144 
1145 	if (!found) {
1146 		int tid;
1147 
1148 		/*
1149 		 * For PS-Poll, this can only happen due to a race condition
1150 		 * when we set the TIM bit and the station notices it, but
1151 		 * before it can poll for the frame we expire it.
1152 		 *
1153 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1154 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1155 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1156 		 *	more than the value specified in the Max SP Length field
1157 		 *	in the QoS Capability element from delivery-enabled ACs,
1158 		 *	that are destined for the non-AP STA.
1159 		 *
1160 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1161 		 */
1162 
1163 		/* This will evaluate to 1, 3, 5 or 7. */
1164 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1165 
1166 		ieee80211_send_null_response(sdata, sta, tid, reason);
1167 		return;
1168 	}
1169 
1170 	if (!driver_release_tids) {
1171 		struct sk_buff_head pending;
1172 		struct sk_buff *skb;
1173 		int num = 0;
1174 		u16 tids = 0;
1175 
1176 		skb_queue_head_init(&pending);
1177 
1178 		while ((skb = __skb_dequeue(&frames))) {
1179 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1180 			struct ieee80211_hdr *hdr = (void *) skb->data;
1181 			u8 *qoshdr = NULL;
1182 
1183 			num++;
1184 
1185 			/*
1186 			 * Tell TX path to send this frame even though the
1187 			 * STA may still remain is PS mode after this frame
1188 			 * exchange.
1189 			 */
1190 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1191 
1192 			/*
1193 			 * Use MoreData flag to indicate whether there are
1194 			 * more buffered frames for this STA
1195 			 */
1196 			if (more_data || !skb_queue_empty(&frames))
1197 				hdr->frame_control |=
1198 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1199 			else
1200 				hdr->frame_control &=
1201 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1202 
1203 			if (ieee80211_is_data_qos(hdr->frame_control) ||
1204 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
1205 				qoshdr = ieee80211_get_qos_ctl(hdr);
1206 
1207 			/* set EOSP for the frame */
1208 			if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
1209 			    qoshdr && skb_queue_empty(&frames))
1210 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1211 
1212 			info->flags |= IEEE80211_TX_STATUS_EOSP |
1213 				       IEEE80211_TX_CTL_REQ_TX_STATUS;
1214 
1215 			if (qoshdr)
1216 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
1217 			else
1218 				tids |= BIT(0);
1219 
1220 			__skb_queue_tail(&pending, skb);
1221 		}
1222 
1223 		drv_allow_buffered_frames(local, sta, tids, num,
1224 					  reason, more_data);
1225 
1226 		ieee80211_add_pending_skbs(local, &pending);
1227 
1228 		sta_info_recalc_tim(sta);
1229 	} else {
1230 		/*
1231 		 * We need to release a frame that is buffered somewhere in the
1232 		 * driver ... it'll have to handle that.
1233 		 * Note that, as per the comment above, it'll also have to see
1234 		 * if there is more than just one frame on the specific TID that
1235 		 * we're releasing from, and it needs to set the more-data bit
1236 		 * accordingly if we tell it that there's no more data. If we do
1237 		 * tell it there's more data, then of course the more-data bit
1238 		 * needs to be set anyway.
1239 		 */
1240 		drv_release_buffered_frames(local, sta, driver_release_tids,
1241 					    n_frames, reason, more_data);
1242 
1243 		/*
1244 		 * Note that we don't recalculate the TIM bit here as it would
1245 		 * most likely have no effect at all unless the driver told us
1246 		 * that the TID became empty before returning here from the
1247 		 * release function.
1248 		 * Either way, however, when the driver tells us that the TID
1249 		 * became empty we'll do the TIM recalculation.
1250 		 */
1251 	}
1252 }
1253 
ieee80211_sta_ps_deliver_poll_response(struct sta_info * sta)1254 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1255 {
1256 	u8 ignore_for_response = sta->sta.uapsd_queues;
1257 
1258 	/*
1259 	 * If all ACs are delivery-enabled then we should reply
1260 	 * from any of them, if only some are enabled we reply
1261 	 * only from the non-enabled ones.
1262 	 */
1263 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1264 		ignore_for_response = 0;
1265 
1266 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1267 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1268 }
1269 
ieee80211_sta_ps_deliver_uapsd(struct sta_info * sta)1270 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1271 {
1272 	int n_frames = sta->sta.max_sp;
1273 	u8 delivery_enabled = sta->sta.uapsd_queues;
1274 
1275 	/*
1276 	 * If we ever grow support for TSPEC this might happen if
1277 	 * the TSPEC update from hostapd comes in between a trigger
1278 	 * frame setting WLAN_STA_UAPSD in the RX path and this
1279 	 * actually getting called.
1280 	 */
1281 	if (!delivery_enabled)
1282 		return;
1283 
1284 	switch (sta->sta.max_sp) {
1285 	case 1:
1286 		n_frames = 2;
1287 		break;
1288 	case 2:
1289 		n_frames = 4;
1290 		break;
1291 	case 3:
1292 		n_frames = 6;
1293 		break;
1294 	case 0:
1295 		/* XXX: what is a good value? */
1296 		n_frames = 8;
1297 		break;
1298 	}
1299 
1300 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1301 					  IEEE80211_FRAME_RELEASE_UAPSD);
1302 }
1303 
ieee80211_sta_block_awake(struct ieee80211_hw * hw,struct ieee80211_sta * pubsta,bool block)1304 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1305 			       struct ieee80211_sta *pubsta, bool block)
1306 {
1307 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1308 
1309 	trace_api_sta_block_awake(sta->local, pubsta, block);
1310 
1311 	if (block)
1312 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1313 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1314 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1315 }
1316 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1317 
ieee80211_sta_eosp_irqsafe(struct ieee80211_sta * pubsta)1318 void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
1319 {
1320 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1321 	struct ieee80211_local *local = sta->local;
1322 	struct sk_buff *skb;
1323 	struct skb_eosp_msg_data *data;
1324 
1325 	trace_api_eosp(local, pubsta);
1326 
1327 	skb = alloc_skb(0, GFP_ATOMIC);
1328 	if (!skb) {
1329 		/* too bad ... but race is better than loss */
1330 		clear_sta_flag(sta, WLAN_STA_SP);
1331 		return;
1332 	}
1333 
1334 	data = (void *)skb->cb;
1335 	memcpy(data->sta, pubsta->addr, ETH_ALEN);
1336 	memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
1337 	skb->pkt_type = IEEE80211_EOSP_MSG;
1338 	skb_queue_tail(&local->skb_queue, skb);
1339 	tasklet_schedule(&local->tasklet);
1340 }
1341 EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
1342 
ieee80211_sta_set_buffered(struct ieee80211_sta * pubsta,u8 tid,bool buffered)1343 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1344 				u8 tid, bool buffered)
1345 {
1346 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1347 
1348 	if (WARN_ON(tid >= STA_TID_NUM))
1349 		return;
1350 
1351 	if (buffered)
1352 		set_bit(tid, &sta->driver_buffered_tids);
1353 	else
1354 		clear_bit(tid, &sta->driver_buffered_tids);
1355 
1356 	sta_info_recalc_tim(sta);
1357 }
1358 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1359 
sta_info_move_state(struct sta_info * sta,enum ieee80211_sta_state new_state)1360 int sta_info_move_state(struct sta_info *sta,
1361 			enum ieee80211_sta_state new_state)
1362 {
1363 	might_sleep();
1364 
1365 	if (sta->sta_state == new_state)
1366 		return 0;
1367 
1368 	/* check allowed transitions first */
1369 
1370 	switch (new_state) {
1371 	case IEEE80211_STA_NONE:
1372 		if (sta->sta_state != IEEE80211_STA_AUTH)
1373 			return -EINVAL;
1374 		break;
1375 	case IEEE80211_STA_AUTH:
1376 		if (sta->sta_state != IEEE80211_STA_NONE &&
1377 		    sta->sta_state != IEEE80211_STA_ASSOC)
1378 			return -EINVAL;
1379 		break;
1380 	case IEEE80211_STA_ASSOC:
1381 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1382 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1383 			return -EINVAL;
1384 		break;
1385 	case IEEE80211_STA_AUTHORIZED:
1386 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1387 			return -EINVAL;
1388 		break;
1389 	default:
1390 		WARN(1, "invalid state %d", new_state);
1391 		return -EINVAL;
1392 	}
1393 
1394 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1395 	printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
1396 		sta->sdata->name, sta->sta.addr, new_state);
1397 #endif
1398 
1399 	/*
1400 	 * notify the driver before the actual changes so it can
1401 	 * fail the transition
1402 	 */
1403 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1404 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1405 					sta->sta_state, new_state);
1406 		if (err)
1407 			return err;
1408 	}
1409 
1410 	/* reflect the change in all state variables */
1411 
1412 	switch (new_state) {
1413 	case IEEE80211_STA_NONE:
1414 		if (sta->sta_state == IEEE80211_STA_AUTH)
1415 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1416 		break;
1417 	case IEEE80211_STA_AUTH:
1418 		if (sta->sta_state == IEEE80211_STA_NONE)
1419 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1420 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1421 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1422 		break;
1423 	case IEEE80211_STA_ASSOC:
1424 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1425 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1426 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1427 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1428 				atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
1429 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1430 		}
1431 		break;
1432 	case IEEE80211_STA_AUTHORIZED:
1433 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
1434 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1435 				atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
1436 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1437 		}
1438 		break;
1439 	default:
1440 		break;
1441 	}
1442 
1443 	sta->sta_state = new_state;
1444 
1445 	return 0;
1446 }
1447