xref: /linux/net/mac80211/agg-rx.c (revision 3e9201e4fe8bd78f4601a51212562505bbb60e3a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HT handling
4  *
5  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
6  * Copyright 2002-2005, Instant802 Networks, Inc.
7  * Copyright 2005-2006, Devicescape Software, Inc.
8  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
9  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
10  * Copyright 2007-2010, Intel Corporation
11  * Copyright(c) 2015-2017 Intel Deutschland GmbH
12  * Copyright (C) 2018-2026 Intel Corporation
13  */
14 
15 /**
16  * DOC: RX A-MPDU aggregation
17  *
18  * Aggregation on the RX side requires only implementing the
19  * @ampdu_action callback that is invoked to start/stop any
20  * block-ack sessions for RX aggregation.
21  *
22  * When RX aggregation is started by the peer, the driver is
23  * notified via @ampdu_action function, with the
24  * %IEEE80211_AMPDU_RX_START action, and may reject the request
25  * in which case a negative response is sent to the peer, if it
26  * accepts it a positive response is sent.
27  *
28  * While the session is active, the device/driver are required
29  * to de-aggregate frames and pass them up one by one to mac80211,
30  * which will handle the reorder buffer.
31  *
32  * When the aggregation session is stopped again by the peer or
33  * ourselves, the driver's @ampdu_action function will be called
34  * with the action %IEEE80211_AMPDU_RX_STOP. In this case, the
35  * call must not fail.
36  */
37 
38 #include <linux/ieee80211.h>
39 #include <linux/slab.h>
40 #include <linux/export.h>
41 #include <net/mac80211.h>
42 #include "ieee80211_i.h"
43 #include "driver-ops.h"
44 
45 static void ieee80211_free_tid_rx(struct rcu_head *h)
46 {
47 	struct tid_ampdu_rx *tid_rx =
48 		container_of(h, struct tid_ampdu_rx, rcu_head);
49 	int i;
50 
51 	for (i = 0; i < tid_rx->buf_size; i++)
52 		__skb_queue_purge(&tid_rx->reorder[i].buf);
53 	kfree(tid_rx);
54 }
55 
56 void __ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
57 				    u16 initiator, u16 reason, bool tx)
58 {
59 	struct ieee80211_local *local = sta->local;
60 	struct tid_ampdu_rx *tid_rx;
61 	struct ieee80211_ampdu_params params = {
62 		.sta = &sta->sta,
63 		.action = IEEE80211_AMPDU_RX_STOP,
64 		.tid = tid,
65 		.amsdu = false,
66 		.timeout = 0,
67 		.ssn = 0,
68 	};
69 
70 	lockdep_assert_wiphy(sta->local->hw.wiphy);
71 
72 	tid_rx = rcu_dereference_protected(sta->ampdu_mlme.tid_rx[tid],
73 					lockdep_is_held(&sta->local->hw.wiphy->mtx));
74 
75 	if (!test_bit(tid, sta->ampdu_mlme.agg_session_valid))
76 		return;
77 
78 	RCU_INIT_POINTER(sta->ampdu_mlme.tid_rx[tid], NULL);
79 	__clear_bit(tid, sta->ampdu_mlme.agg_session_valid);
80 
81 	ht_dbg(sta->sdata,
82 	       "Rx BA session stop requested for %pM tid %u %s reason: %d\n",
83 	       sta->sta.addr, tid,
84 	       initiator == WLAN_BACK_RECIPIENT ? "recipient" : "initiator",
85 	       (int)reason);
86 
87 	if (drv_ampdu_action(local, sta->sdata, &params))
88 		sdata_info(sta->sdata,
89 			   "HW problem - can not stop rx aggregation for %pM tid %d\n",
90 			   sta->sta.addr, tid);
91 
92 	/* check if this is a self generated aggregation halt */
93 	if (initiator == WLAN_BACK_RECIPIENT && tx)
94 		ieee80211_send_delba(sta->sdata, sta->sta.addr,
95 				     tid, WLAN_BACK_RECIPIENT, reason,
96 				     ieee80211_s1g_use_ndp_ba(sta->sdata, sta));
97 
98 	/*
99 	 * return here in case tid_rx is not assigned - which will happen if
100 	 * IEEE80211_HW_SUPPORTS_REORDERING_BUFFER is set.
101 	 */
102 	if (!tid_rx)
103 		return;
104 
105 	timer_delete_sync(&tid_rx->session_timer);
106 
107 	/* make sure ieee80211_sta_reorder_release() doesn't re-arm the timer */
108 	spin_lock_bh(&tid_rx->reorder_lock);
109 	tid_rx->removed = true;
110 	spin_unlock_bh(&tid_rx->reorder_lock);
111 	timer_delete_sync(&tid_rx->reorder_timer);
112 
113 	call_rcu(&tid_rx->rcu_head, ieee80211_free_tid_rx);
114 }
115 
116 void ieee80211_stop_rx_ba_session(struct ieee80211_vif *vif, u16 ba_rx_bitmap,
117 				  const u8 *addr)
118 {
119 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
120 	struct sta_info *sta;
121 	int i;
122 
123 	rcu_read_lock();
124 	sta = sta_info_get_bss(sdata, addr);
125 	if (!sta) {
126 		rcu_read_unlock();
127 		return;
128 	}
129 
130 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
131 		if (ba_rx_bitmap & BIT(i))
132 			set_bit(i, sta->ampdu_mlme.tid_rx_stop_requested);
133 
134 	wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
135 	rcu_read_unlock();
136 }
137 EXPORT_SYMBOL(ieee80211_stop_rx_ba_session);
138 
139 /*
140  * After accepting the AddBA Request we activated a timer,
141  * resetting it after each frame that arrives from the originator.
142  */
143 static void sta_rx_agg_session_timer_expired(struct timer_list *t)
144 {
145 	struct tid_ampdu_rx *tid_rx = timer_container_of(tid_rx, t,
146 							 session_timer);
147 	struct sta_info *sta = tid_rx->sta;
148 	u8 tid = tid_rx->tid;
149 	unsigned long timeout;
150 
151 	timeout = tid_rx->last_rx + TU_TO_JIFFIES(tid_rx->timeout);
152 	if (time_is_after_jiffies(timeout)) {
153 		mod_timer(&tid_rx->session_timer, timeout);
154 		return;
155 	}
156 
157 	ht_dbg(sta->sdata, "RX session timer expired on %pM tid %d\n",
158 	       sta->sta.addr, tid);
159 
160 	set_bit(tid, sta->ampdu_mlme.tid_rx_timer_expired);
161 	wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
162 }
163 
164 static void sta_rx_agg_reorder_timer_expired(struct timer_list *t)
165 {
166 	struct tid_ampdu_rx *tid_rx = timer_container_of(tid_rx, t,
167 							 reorder_timer);
168 
169 	rcu_read_lock();
170 	ieee80211_release_reorder_timeout(tid_rx->sta, tid_rx->tid);
171 	rcu_read_unlock();
172 }
173 
174 void ieee80211_add_addbaext(struct sk_buff *skb,
175 			    const u8 req_addba_ext_data,
176 			    u16 buf_size)
177 {
178 	struct ieee80211_addba_ext_ie *addba_ext;
179 	u8 *pos;
180 
181 	pos = skb_put_zero(skb, 2 + sizeof(struct ieee80211_addba_ext_ie));
182 	*pos++ = WLAN_EID_ADDBA_EXT;
183 	*pos++ = sizeof(struct ieee80211_addba_ext_ie);
184 	addba_ext = (struct ieee80211_addba_ext_ie *)pos;
185 
186 	addba_ext->data = IEEE80211_ADDBA_EXT_NO_FRAG;
187 	if (req_addba_ext_data)
188 		addba_ext->data &= req_addba_ext_data;
189 
190 	addba_ext->data |=
191 		u8_encode_bits(buf_size >> IEEE80211_ADDBA_EXT_BUF_SIZE_SHIFT,
192 			       IEEE80211_ADDBA_EXT_BUF_SIZE_MASK);
193 }
194 
195 u8 ieee80211_retrieve_addba_ext_data(struct sta_info *sta,
196 				     const void *elem_data, ssize_t elem_len,
197 				     u16 *buf_size)
198 {
199 	struct ieee802_11_elems *elems;
200 	u8 buf_size_1k, data = 0;
201 
202 	if (!sta->sta.deflink.he_cap.has_he)
203 		return 0;
204 
205 	if (elem_len <= 0)
206 		return 0;
207 
208 	elems = ieee802_11_parse_elems(elem_data, elem_len,
209 				       IEEE80211_FTYPE_MGMT |
210 				       IEEE80211_STYPE_ACTION,
211 				       NULL);
212 
213 	if (!elems || elems->parse_error || !elems->addba_ext_ie)
214 		goto free;
215 
216 	data = elems->addba_ext_ie->data;
217 
218 	if (buf_size &&
219 	    (sta->sta.valid_links || sta->sta.deflink.eht_cap.has_eht)) {
220 		buf_size_1k = u8_get_bits(elems->addba_ext_ie->data,
221 					  IEEE80211_ADDBA_EXT_BUF_SIZE_MASK);
222 		*buf_size |= (u16)buf_size_1k <<
223 			     IEEE80211_ADDBA_EXT_BUF_SIZE_SHIFT;
224 	}
225 
226 free:
227 	kfree(elems);
228 
229 	return data;
230 }
231 
232 static void ieee80211_send_addba_resp(struct sta_info *sta, u8 *da, u16 tid,
233 				      u8 dialog_token, u16 status, u16 policy,
234 				      u16 buf_size, u16 timeout,
235 				      const u8 req_addba_ext_data)
236 {
237 	struct ieee80211_sub_if_data *sdata = sta->sdata;
238 	struct ieee80211_local *local = sdata->local;
239 	struct sk_buff *skb;
240 	struct ieee80211_mgmt *mgmt;
241 	bool amsdu = ieee80211_hw_check(&local->hw, SUPPORTS_AMSDU_IN_AMPDU);
242 	bool use_ndp = ieee80211_s1g_use_ndp_ba(sdata, sta);
243 	u16 capab;
244 
245 	skb = dev_alloc_skb(sizeof(*mgmt) +
246 		    2 + sizeof(struct ieee80211_addba_ext_ie) +
247 		    local->hw.extra_tx_headroom);
248 	if (!skb)
249 		return;
250 
251 	skb_reserve(skb, local->hw.extra_tx_headroom);
252 	mgmt = ieee80211_mgmt_ba(skb, da, sdata);
253 
254 	skb_put(skb, 2 + sizeof(mgmt->u.action.addba_resp));
255 	mgmt->u.action.category = WLAN_CATEGORY_BACK;
256 	mgmt->u.action.action_code = use_ndp ?
257 		WLAN_ACTION_NDP_ADDBA_RESP : WLAN_ACTION_ADDBA_RESP;
258 
259 	mgmt->u.action.addba_resp.dialog_token = dialog_token;
260 
261 	capab = u16_encode_bits(amsdu, IEEE80211_ADDBA_PARAM_AMSDU_MASK);
262 	capab |= u16_encode_bits(policy, IEEE80211_ADDBA_PARAM_POLICY_MASK);
263 	capab |= u16_encode_bits(tid, IEEE80211_ADDBA_PARAM_TID_MASK);
264 	capab |= u16_encode_bits(buf_size, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
265 
266 	mgmt->u.action.addba_resp.capab = cpu_to_le16(capab);
267 	mgmt->u.action.addba_resp.timeout = cpu_to_le16(timeout);
268 	mgmt->u.action.addba_resp.status = cpu_to_le16(status);
269 
270 	if (sta->sta.valid_links || sta->sta.deflink.he_cap.has_he)
271 		ieee80211_add_addbaext(skb, req_addba_ext_data, buf_size);
272 
273 	ieee80211_tx_skb(sdata, skb);
274 }
275 
276 void __ieee80211_start_rx_ba_session(struct sta_info *sta,
277 				     u8 dialog_token, u16 timeout,
278 				     u16 start_seq_num, u16 ba_policy, u16 tid,
279 				     u16 buf_size, bool tx, bool auto_seq,
280 				     bool req_ndp,
281 				     const u8 addba_ext_data)
282 {
283 	struct ieee80211_local *local = sta->sdata->local;
284 	struct tid_ampdu_rx *tid_agg_rx;
285 	struct ieee80211_ampdu_params params = {
286 		.sta = &sta->sta,
287 		.action = IEEE80211_AMPDU_RX_START,
288 		.tid = tid,
289 		.amsdu = false,
290 		.timeout = timeout,
291 		.ssn = start_seq_num,
292 	};
293 	int i, ret = -EOPNOTSUPP;
294 	u16 status = WLAN_STATUS_REQUEST_DECLINED;
295 	u16 max_buf_size;
296 
297 	lockdep_assert_wiphy(sta->local->hw.wiphy);
298 
299 	if (tid >= IEEE80211_FIRST_TSPEC_TSID) {
300 		ht_dbg(sta->sdata,
301 		       "STA %pM requests BA session on unsupported tid %d\n",
302 		       sta->sta.addr, tid);
303 		goto end;
304 	}
305 
306 	if (tx && ieee80211_s1g_use_ndp_ba(sta->sdata, sta) && !req_ndp) {
307 		/*
308 		 * According to IEEE 802.11-2024: Inform S1G originator
309 		 * ADDBA rejected as NDP BlockAck is preferred
310 		 */
311 		status = WLAN_STATUS_REJECTED_NDP_BLOCK_ACK_SUGGESTED;
312 		ht_dbg(sta->sdata,
313 		       "Rejecting AddBA Req from %pM tid %u - require NDP BlockAck\n",
314 		       sta->sta.addr, tid);
315 		goto end;
316 	}
317 
318 	if (!sta->sta.valid_links &&
319 	    !sta->sta.deflink.ht_cap.ht_supported &&
320 	    !sta->sta.deflink.he_cap.has_he &&
321 	    !sta->sta.deflink.s1g_cap.s1g) {
322 		ht_dbg(sta->sdata,
323 		       "STA %pM erroneously requests BA session on tid %d w/o HT\n",
324 		       sta->sta.addr, tid);
325 		/* send a response anyway, it's an error case if we get here */
326 		goto end;
327 	}
328 
329 	if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
330 		ht_dbg(sta->sdata,
331 		       "Suspend in progress - Denying ADDBA request (%pM tid %d)\n",
332 		       sta->sta.addr, tid);
333 		goto end;
334 	}
335 
336 	if (sta->sta.valid_links || sta->sta.deflink.eht_cap.has_eht)
337 		max_buf_size = IEEE80211_MAX_AMPDU_BUF_EHT;
338 	else if (sta->sta.deflink.he_cap.has_he)
339 		max_buf_size = IEEE80211_MAX_AMPDU_BUF_HE;
340 	else
341 		max_buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
342 
343 	/* sanity check for incoming parameters:
344 	 * check if configuration can support the BA policy
345 	 * and if buffer size does not exceeds max value */
346 	/* XXX: check own ht delayed BA capability?? */
347 	if (((ba_policy != 1) &&
348 	     (sta->sta.valid_links ||
349 	      !(sta->sta.deflink.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA) ||
350 	      !(sta->sta.deflink.s1g_cap.cap[3] & S1G_CAP3_HT_DELAYED_BA))) ||
351 	    (buf_size > max_buf_size)) {
352 		status = WLAN_STATUS_INVALID_QOS_PARAM;
353 		ht_dbg_ratelimited(sta->sdata,
354 				   "AddBA Req with bad params from %pM on tid %u. policy %d, buffer size %d\n",
355 				   sta->sta.addr, tid, ba_policy, buf_size);
356 		goto end;
357 	}
358 	/* determine default buffer size */
359 	if (buf_size == 0)
360 		buf_size = max_buf_size;
361 
362 	/* make sure the size doesn't exceed the maximum supported by the hw */
363 	if (buf_size > sta->sta.max_rx_aggregation_subframes)
364 		buf_size = sta->sta.max_rx_aggregation_subframes;
365 	params.buf_size = buf_size;
366 
367 	ht_dbg(sta->sdata, "AddBA Req buf_size=%d for %pM\n",
368 	       buf_size, sta->sta.addr);
369 
370 	if (test_bit(tid, sta->ampdu_mlme.agg_session_valid)) {
371 		if (sta->ampdu_mlme.tid_rx_token[tid] == dialog_token) {
372 			struct tid_ampdu_rx *tid_rx;
373 
374 			ht_dbg_ratelimited(sta->sdata,
375 					   "updated AddBA Req from %pM on tid %u\n",
376 					   sta->sta.addr, tid);
377 			/* We have no API to update the timeout value in the
378 			 * driver so reject the timeout update if the timeout
379 			 * changed. If it did not change, i.e., no real update,
380 			 * just reply with success.
381 			 */
382 			rcu_read_lock();
383 			tid_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
384 			if (tid_rx && tid_rx->timeout == timeout)
385 				status = WLAN_STATUS_SUCCESS;
386 			else
387 				status = WLAN_STATUS_REQUEST_DECLINED;
388 			rcu_read_unlock();
389 			goto end;
390 		}
391 
392 		ht_dbg_ratelimited(sta->sdata,
393 				   "unexpected AddBA Req from %pM on tid %u\n",
394 				   sta->sta.addr, tid);
395 
396 		/* delete existing Rx BA session on the same tid */
397 		__ieee80211_stop_rx_ba_session(sta, tid, WLAN_BACK_RECIPIENT,
398 					       WLAN_STATUS_UNSPECIFIED_QOS,
399 					       false);
400 	}
401 
402 	if (ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER)) {
403 		ret = drv_ampdu_action(local, sta->sdata, &params);
404 		ht_dbg(sta->sdata,
405 		       "Rx A-MPDU request on %pM tid %d result %d\n",
406 		       sta->sta.addr, tid, ret);
407 		if (!ret)
408 			status = WLAN_STATUS_SUCCESS;
409 		goto end;
410 	}
411 
412 	/* prepare A-MPDU MLME for Rx aggregation */
413 	tid_agg_rx = kzalloc_flex(*tid_agg_rx, reorder, buf_size);
414 	if (!tid_agg_rx)
415 		goto end;
416 
417 	spin_lock_init(&tid_agg_rx->reorder_lock);
418 
419 	/* rx timer */
420 	timer_setup(&tid_agg_rx->session_timer,
421 		    sta_rx_agg_session_timer_expired, TIMER_DEFERRABLE);
422 
423 	/* rx reorder timer */
424 	timer_setup(&tid_agg_rx->reorder_timer,
425 		    sta_rx_agg_reorder_timer_expired, 0);
426 
427 	for (i = 0; i < buf_size; i++)
428 		__skb_queue_head_init(&tid_agg_rx->reorder[i].buf);
429 
430 	ret = drv_ampdu_action(local, sta->sdata, &params);
431 	ht_dbg(sta->sdata, "Rx A-MPDU request on %pM tid %d result %d\n",
432 	       sta->sta.addr, tid, ret);
433 	if (ret) {
434 		kfree(tid_agg_rx);
435 		goto end;
436 	}
437 
438 	/* update data */
439 	tid_agg_rx->ssn = start_seq_num;
440 	tid_agg_rx->head_seq_num = start_seq_num;
441 	tid_agg_rx->buf_size = buf_size;
442 	tid_agg_rx->timeout = timeout;
443 	tid_agg_rx->stored_mpdu_num = 0;
444 	tid_agg_rx->auto_seq = auto_seq;
445 	tid_agg_rx->started = false;
446 	tid_agg_rx->reorder_buf_filtered = 0;
447 	tid_agg_rx->tid = tid;
448 	tid_agg_rx->sta = sta;
449 	status = WLAN_STATUS_SUCCESS;
450 
451 	/* activate it for RX */
452 	rcu_assign_pointer(sta->ampdu_mlme.tid_rx[tid], tid_agg_rx);
453 
454 	if (timeout) {
455 		mod_timer(&tid_agg_rx->session_timer, TU_TO_EXP_TIME(timeout));
456 		tid_agg_rx->last_rx = jiffies;
457 	}
458 
459 end:
460 	if (status == WLAN_STATUS_SUCCESS) {
461 		__set_bit(tid, sta->ampdu_mlme.agg_session_valid);
462 		__clear_bit(tid, sta->ampdu_mlme.unexpected_agg);
463 		sta->ampdu_mlme.tid_rx_token[tid] = dialog_token;
464 	}
465 
466 	if (tx)
467 		ieee80211_send_addba_resp(sta, sta->sta.addr, tid,
468 					  dialog_token, status, 1, buf_size,
469 					  timeout, addba_ext_data);
470 }
471 
472 void ieee80211_process_addba_request(struct ieee80211_local *local,
473 				     struct sta_info *sta,
474 				     struct ieee80211_mgmt *mgmt,
475 				     size_t len)
476 {
477 	bool req_ndp = mgmt->u.action.action_code == WLAN_ACTION_NDP_ADDBA_REQ;
478 	u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num;
479 	u8 dialog_token, addba_ext_data;
480 
481 	/* extract session parameters from addba request frame */
482 	dialog_token = mgmt->u.action.addba_req.dialog_token;
483 	timeout = le16_to_cpu(mgmt->u.action.addba_req.timeout);
484 	start_seq_num =
485 		le16_to_cpu(mgmt->u.action.addba_req.start_seq_num) >> 4;
486 
487 	capab = le16_to_cpu(mgmt->u.action.addba_req.capab);
488 	ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
489 	tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
490 	buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
491 
492 	addba_ext_data =
493 		ieee80211_retrieve_addba_ext_data(sta,
494 						  mgmt->u.action.addba_req.variable,
495 						  len -
496 						  offsetof(typeof(*mgmt),
497 							   u.action.addba_req.variable),
498 						  &buf_size);
499 
500 	__ieee80211_start_rx_ba_session(sta, dialog_token, timeout,
501 					start_seq_num, ba_policy, tid,
502 					buf_size, true, false,
503 					req_ndp, addba_ext_data);
504 }
505 
506 void ieee80211_manage_rx_ba_offl(struct ieee80211_vif *vif,
507 				 const u8 *addr, unsigned int tid)
508 {
509 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
510 	struct sta_info *sta;
511 
512 	rcu_read_lock();
513 	sta = sta_info_get_bss(sdata, addr);
514 	if (!sta)
515 		goto unlock;
516 
517 	set_bit(tid, sta->ampdu_mlme.tid_rx_manage_offl);
518 	wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
519  unlock:
520 	rcu_read_unlock();
521 }
522 EXPORT_SYMBOL(ieee80211_manage_rx_ba_offl);
523 
524 void ieee80211_rx_ba_timer_expired(struct ieee80211_vif *vif,
525 				   const u8 *addr, unsigned int tid)
526 {
527 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
528 	struct sta_info *sta;
529 
530 	rcu_read_lock();
531 	sta = sta_info_get_bss(sdata, addr);
532 	if (!sta)
533 		goto unlock;
534 
535 	set_bit(tid, sta->ampdu_mlme.tid_rx_timer_expired);
536 	wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
537 
538  unlock:
539 	rcu_read_unlock();
540 }
541 EXPORT_SYMBOL(ieee80211_rx_ba_timer_expired);
542