Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright 2012-2013, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de>
  4 * Copyright 2012-2013, cozybit Inc.
  5 * Copyright (C) 2021 Intel Corporation
 
  6 */
  7
  8#include "mesh.h"
  9#include "wme.h"
 10
 11
 12/* mesh PS management */
 13
 14/**
 15 * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave
 16 * @sta: the station to get the frame for
 
 
 17 */
 18static struct sk_buff *mps_qos_null_get(struct sta_info *sta)
 19{
 20	struct ieee80211_sub_if_data *sdata = sta->sdata;
 21	struct ieee80211_local *local = sdata->local;
 22	struct ieee80211_hdr *nullfunc; /* use 4addr header */
 23	struct sk_buff *skb;
 24	int size = sizeof(*nullfunc);
 25	__le16 fc;
 26
 27	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2);
 28	if (!skb)
 29		return NULL;
 30	skb_reserve(skb, local->hw.extra_tx_headroom);
 31
 32	nullfunc = skb_put(skb, size);
 33	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_NULLFUNC);
 34	ieee80211_fill_mesh_addresses(nullfunc, &fc, sta->sta.addr,
 35				      sdata->vif.addr);
 36	nullfunc->frame_control = fc;
 37	nullfunc->duration_id = 0;
 38	nullfunc->seq_ctrl = 0;
 39	/* no address resolution for this frame -> set addr 1 immediately */
 40	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
 41	skb_put_zero(skb, 2); /* append QoS control field */
 42	ieee80211_mps_set_frame_flags(sdata, sta, nullfunc);
 43
 44	return skb;
 45}
 46
 47/**
 48 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
 49 * @sta: the station to send to
 50 */
 51static void mps_qos_null_tx(struct sta_info *sta)
 52{
 53	struct sk_buff *skb;
 54
 55	skb = mps_qos_null_get(sta);
 56	if (!skb)
 57		return;
 58
 59	mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
 60		sta->sta.addr);
 61
 62	/* don't unintentionally start a MPSP */
 63	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
 64		u8 *qc = ieee80211_get_qos_ctl((void *) skb->data);
 65
 66		qc[0] |= IEEE80211_QOS_CTL_EOSP;
 67	}
 68
 69	ieee80211_tx_skb(sta->sdata, skb);
 70}
 71
 72/**
 73 * ieee80211_mps_local_status_update - track status of local link-specific PMs
 74 *
 75 * @sdata: local mesh subif
 76 *
 77 * sets the non-peer power mode and triggers the driver PS (re-)configuration
 78 * Return BSS_CHANGED_BEACON if a beacon update is necessary.
 
 
 79 */
 80u32 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
 81{
 82	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
 83	struct sta_info *sta;
 84	bool peering = false;
 85	int light_sleep_cnt = 0;
 86	int deep_sleep_cnt = 0;
 87	u32 changed = 0;
 88	enum nl80211_mesh_power_mode nonpeer_pm;
 89
 90	rcu_read_lock();
 91	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
 92		if (sdata != sta->sdata)
 93			continue;
 94
 95		switch (sta->mesh->plink_state) {
 96		case NL80211_PLINK_OPN_SNT:
 97		case NL80211_PLINK_OPN_RCVD:
 98		case NL80211_PLINK_CNF_RCVD:
 99			peering = true;
100			break;
101		case NL80211_PLINK_ESTAB:
102			if (sta->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
103				light_sleep_cnt++;
104			else if (sta->mesh->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
105				deep_sleep_cnt++;
106			break;
107		default:
108			break;
109		}
110	}
111	rcu_read_unlock();
112
113	/*
114	 * Set non-peer mode to active during peering/scanning/authentication
115	 * (see IEEE802.11-2012 13.14.8.3). The non-peer mesh power mode is
116	 * deep sleep if the local STA is in light or deep sleep towards at
117	 * least one mesh peer (see 13.14.3.1). Otherwise, set it to the
118	 * user-configured default value.
119	 */
120	if (peering) {
121		mps_dbg(sdata, "setting non-peer PM to active for peering\n");
122		nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
123	} else if (light_sleep_cnt || deep_sleep_cnt) {
124		mps_dbg(sdata, "setting non-peer PM to deep sleep\n");
125		nonpeer_pm = NL80211_MESH_POWER_DEEP_SLEEP;
126	} else {
127		mps_dbg(sdata, "setting non-peer PM to user value\n");
128		nonpeer_pm = ifmsh->mshcfg.power_mode;
129	}
130
131	/* need update if sleep counts move between 0 and non-zero */
132	if (ifmsh->nonpeer_pm != nonpeer_pm ||
133	    !ifmsh->ps_peers_light_sleep != !light_sleep_cnt ||
134	    !ifmsh->ps_peers_deep_sleep != !deep_sleep_cnt)
135		changed = BSS_CHANGED_BEACON;
136
137	ifmsh->nonpeer_pm = nonpeer_pm;
138	ifmsh->ps_peers_light_sleep = light_sleep_cnt;
139	ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
140
141	return changed;
142}
143
144/**
145 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
146 *
147 * @sta: mesh STA
148 * @pm: the power mode to set
149 * Return BSS_CHANGED_BEACON if a beacon update is in order.
150 */
151u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
152				   enum nl80211_mesh_power_mode pm)
153{
154	struct ieee80211_sub_if_data *sdata = sta->sdata;
155
156	if (sta->mesh->local_pm == pm)
157		return 0;
158
159	mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
160		pm, sta->sta.addr);
161
162	sta->mesh->local_pm = pm;
163
164	/*
165	 * announce peer-specific power mode transition
166	 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
167	 */
168	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
169		mps_qos_null_tx(sta);
170
171	return ieee80211_mps_local_status_update(sdata);
172}
173
174/**
175 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
176 *
177 * @sdata: local mesh subif
178 * @sta: mesh STA
179 * @hdr: 802.11 frame header
180 *
181 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
182 *
183 * NOTE: sta must be given when an individually-addressed QoS frame header
184 * is handled, for group-addressed and management frames it is not used
185 */
186void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
187				   struct sta_info *sta,
188				   struct ieee80211_hdr *hdr)
189{
190	enum nl80211_mesh_power_mode pm;
191	u8 *qc;
192
193	if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
194		    ieee80211_is_data_qos(hdr->frame_control) &&
195		    !sta))
196		return;
197
198	if (is_unicast_ether_addr(hdr->addr1) &&
199	    ieee80211_is_data_qos(hdr->frame_control) &&
200	    sta->mesh->plink_state == NL80211_PLINK_ESTAB)
201		pm = sta->mesh->local_pm;
202	else
203		pm = sdata->u.mesh.nonpeer_pm;
204
205	if (pm == NL80211_MESH_POWER_ACTIVE)
206		hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
207	else
208		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
209
210	if (!ieee80211_is_data_qos(hdr->frame_control))
211		return;
212
213	qc = ieee80211_get_qos_ctl(hdr);
214
215	if ((is_unicast_ether_addr(hdr->addr1) &&
216	     pm == NL80211_MESH_POWER_DEEP_SLEEP) ||
217	    (is_multicast_ether_addr(hdr->addr1) &&
218	     sdata->u.mesh.ps_peers_deep_sleep > 0))
219		qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
220	else
221		qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
222}
223
224/**
225 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
226 *
227 * @sta: mesh STA
228 *
229 * called after change of peering status or non-peer/peer-specific power mode
230 */
231void ieee80211_mps_sta_status_update(struct sta_info *sta)
232{
233	enum nl80211_mesh_power_mode pm;
234	bool do_buffer;
235
236	/* For non-assoc STA, prevent buffering or frame transmission */
237	if (sta->sta_state < IEEE80211_STA_ASSOC)
238		return;
239
240	/*
241	 * use peer-specific power mode if peering is established and the
242	 * peer's power mode is known
243	 */
244	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
245	    sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN)
246		pm = sta->mesh->peer_pm;
247	else
248		pm = sta->mesh->nonpeer_pm;
249
250	do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
251
252	/* clear the MPSP flags for non-peers or active STA */
253	if (sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
254		clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
255		clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
256	} else if (!do_buffer) {
257		clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
258	}
259
260	/* Don't let the same PS state be set twice */
261	if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
262		return;
263
264	if (do_buffer) {
265		set_sta_flag(sta, WLAN_STA_PS_STA);
266		atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps);
267		mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n",
268			sta->sta.addr);
269	} else {
270		ieee80211_sta_ps_deliver_wakeup(sta);
271	}
272}
273
274static void mps_set_sta_peer_pm(struct sta_info *sta,
275				struct ieee80211_hdr *hdr)
276{
277	enum nl80211_mesh_power_mode pm;
278	u8 *qc = ieee80211_get_qos_ctl(hdr);
279
280	/*
281	 * Test Power Management field of frame control (PW) and
282	 * mesh power save level subfield of QoS control field (PSL)
283	 *
284	 * | PM | PSL| Mesh PM |
285	 * +----+----+---------+
286	 * | 0  |Rsrv|  Active |
287	 * | 1  | 0  |  Light  |
288	 * | 1  | 1  |  Deep   |
289	 */
290	if (ieee80211_has_pm(hdr->frame_control)) {
291		if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8))
292			pm = NL80211_MESH_POWER_DEEP_SLEEP;
293		else
294			pm = NL80211_MESH_POWER_LIGHT_SLEEP;
295	} else {
296		pm = NL80211_MESH_POWER_ACTIVE;
297	}
298
299	if (sta->mesh->peer_pm == pm)
300		return;
301
302	mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
303		sta->sta.addr, pm);
304
305	sta->mesh->peer_pm = pm;
306
307	ieee80211_mps_sta_status_update(sta);
308}
309
310static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
311				   struct ieee80211_hdr *hdr)
312{
313	enum nl80211_mesh_power_mode pm;
314
315	if (ieee80211_has_pm(hdr->frame_control))
316		pm = NL80211_MESH_POWER_DEEP_SLEEP;
317	else
318		pm = NL80211_MESH_POWER_ACTIVE;
319
320	if (sta->mesh->nonpeer_pm == pm)
321		return;
322
323	mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
324		sta->sta.addr, pm);
325
326	sta->mesh->nonpeer_pm = pm;
327
328	ieee80211_mps_sta_status_update(sta);
329}
330
331/**
332 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
333 *
334 * @sta: STA info that transmitted the frame
335 * @hdr: IEEE 802.11 (QoS) Header
336 */
337void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
338				    struct ieee80211_hdr *hdr)
339{
340	if (is_unicast_ether_addr(hdr->addr1) &&
341	    ieee80211_is_data_qos(hdr->frame_control)) {
342		/*
343		 * individually addressed QoS Data/Null frames contain
344		 * peer link-specific PS mode towards the local STA
345		 */
346		mps_set_sta_peer_pm(sta, hdr);
347
348		/* check for mesh Peer Service Period trigger frames */
349		ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
350					       sta, false, false);
351	} else {
352		/*
353		 * can only determine non-peer PS mode
354		 * (see IEEE802.11-2012 8.2.4.1.7)
355		 */
356		mps_set_sta_nonpeer_pm(sta, hdr);
357	}
358}
359
360
361/* mesh PS frame release */
362
363static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
364{
365	struct ieee80211_sub_if_data *sdata = sta->sdata;
366	struct sk_buff *skb;
367	struct ieee80211_hdr *nullfunc;
368	struct ieee80211_tx_info *info;
369	u8 *qc;
370
371	skb = mps_qos_null_get(sta);
372	if (!skb)
373		return;
374
375	nullfunc = (struct ieee80211_hdr *) skb->data;
376	if (!eosp)
377		nullfunc->frame_control |=
378				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
379	/*
380	 * | RSPI | EOSP |  MPSP triggering   |
381	 * +------+------+--------------------+
382	 * |  0   |  0   | local STA is owner |
383	 * |  0   |  1   | no MPSP (MPSP end) |
384	 * |  1   |  0   | both STA are owner |
385	 * |  1   |  1   | peer STA is owner  | see IEEE802.11-2012 13.14.9.2
386	 */
387	qc = ieee80211_get_qos_ctl(nullfunc);
388	if (rspi)
389		qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
390	if (eosp)
391		qc[0] |= IEEE80211_QOS_CTL_EOSP;
392
393	info = IEEE80211_SKB_CB(skb);
394
395	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
396		       IEEE80211_TX_CTL_REQ_TX_STATUS;
397
398	mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
399		rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
400
401	ieee80211_tx_skb(sdata, skb);
402}
403
404/**
405 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
406 * @sta: the station to handle
407 * @frames: the frame list to append to
408 *
409 * To properly end a mesh MPSP the last transmitted frame has to set the EOSP
410 * flag in the QoS Control field. In case the current tailing frame is not a
411 * QoS Data frame, append a QoS Null to carry the flag.
412 */
413static void mpsp_qos_null_append(struct sta_info *sta,
414				 struct sk_buff_head *frames)
415{
416	struct ieee80211_sub_if_data *sdata = sta->sdata;
417	struct sk_buff *new_skb, *skb = skb_peek_tail(frames);
418	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
419	struct ieee80211_tx_info *info;
420
421	if (ieee80211_is_data_qos(hdr->frame_control))
422		return;
423
424	new_skb = mps_qos_null_get(sta);
425	if (!new_skb)
426		return;
427
428	mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
429		sta->sta.addr);
430	/*
431	 * This frame has to be transmitted last. Assign lowest priority to
432	 * make sure it cannot pass other frames when releasing multiple ACs.
433	 */
434	new_skb->priority = 1;
435	skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
436	ieee80211_set_qos_hdr(sdata, new_skb);
437
438	info = IEEE80211_SKB_CB(new_skb);
439	info->control.vif = &sdata->vif;
440	info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
441
442	__skb_queue_tail(frames, new_skb);
443}
444
445/**
446 * mps_frame_deliver - transmit frames during mesh powersave
447 *
448 * @sta: STA info to transmit to
449 * @n_frames: number of frames to transmit. -1 for all
450 */
451static void mps_frame_deliver(struct sta_info *sta, int n_frames)
452{
453	struct ieee80211_local *local = sta->sdata->local;
454	int ac;
455	struct sk_buff_head frames;
456	struct sk_buff *skb;
457	bool more_data = false;
458
459	skb_queue_head_init(&frames);
460
461	/* collect frame(s) from buffers */
462	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
463		while (n_frames != 0) {
464			skb = skb_dequeue(&sta->tx_filtered[ac]);
465			if (!skb) {
466				skb = skb_dequeue(
467					&sta->ps_tx_buf[ac]);
468				if (skb)
469					local->total_ps_buffered--;
470			}
471			if (!skb)
472				break;
473			n_frames--;
474			__skb_queue_tail(&frames, skb);
475		}
476
477		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
478		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
479			more_data = true;
480	}
481
482	/* nothing to send? -> EOSP */
483	if (skb_queue_empty(&frames)) {
484		mpsp_trigger_send(sta, false, true);
485		return;
486	}
487
488	/* in a MPSP make sure the last skb is a QoS Data frame */
489	if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
490		mpsp_qos_null_append(sta, &frames);
491
492	mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
493		skb_queue_len(&frames), sta->sta.addr);
494
495	/* prepare collected frames for transmission */
496	skb_queue_walk(&frames, skb) {
497		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
498		struct ieee80211_hdr *hdr = (void *) skb->data;
499
500		/*
501		 * Tell TX path to send this frame even though the
502		 * STA may still remain is PS mode after this frame
503		 * exchange.
504		 */
505		info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
506
507		if (more_data || !skb_queue_is_last(&frames, skb))
508			hdr->frame_control |=
509				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
510		else
511			hdr->frame_control &=
512				cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
513
514		if (skb_queue_is_last(&frames, skb) &&
515		    ieee80211_is_data_qos(hdr->frame_control)) {
516			u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
517
518			/* MPSP trigger frame ends service period */
519			*qoshdr |= IEEE80211_QOS_CTL_EOSP;
520			info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
521		}
522	}
523
524	ieee80211_add_pending_skbs(local, &frames);
525	sta_info_recalc_tim(sta);
526}
527
528/**
529 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
530 *
531 * @qc: QoS Control field
532 * @sta: peer to start a MPSP with
533 * @tx: frame was transmitted by the local STA
534 * @acked: frame has been transmitted successfully
535 *
536 * NOTE: active mode STA may only serve as MPSP owner
537 */
538void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
539				    bool tx, bool acked)
540{
541	u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
542	u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
543
544	if (tx) {
545		if (rspi && acked)
546			set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
547
548		if (eosp)
549			clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
550		else if (acked &&
551			 test_sta_flag(sta, WLAN_STA_PS_STA) &&
552			 !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
553			mps_frame_deliver(sta, -1);
554	} else {
555		if (eosp)
556			clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
557		else if (sta->mesh->local_pm != NL80211_MESH_POWER_ACTIVE)
558			set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
559
560		if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
561			mps_frame_deliver(sta, -1);
562	}
563}
564
565/**
566 * ieee80211_mps_frame_release - release frames buffered due to mesh power save
567 *
568 * @sta: mesh STA
569 * @elems: IEs of beacon or probe response
570 *
571 * For peers if we have individually-addressed frames buffered or the peer
572 * indicates buffered frames, send a corresponding MPSP trigger frame. Since
573 * we do not evaluate the awake window duration, QoS Nulls are used as MPSP
574 * trigger frames. If the neighbour STA is not a peer, only send single frames.
575 */
576void ieee80211_mps_frame_release(struct sta_info *sta,
577				 struct ieee802_11_elems *elems)
578{
579	int ac, buffer_local = 0;
580	bool has_buffered = false;
581
582	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
583		has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
584						   sta->mesh->aid);
585
586	if (has_buffered)
587		mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
588			sta->sta.addr);
589
590	/* only transmit to PS STA with announced, non-zero awake window */
591	if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
592	    (!elems->awake_window || !get_unaligned_le16(elems->awake_window)))
593		return;
594
595	if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
596		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
597			buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
598					skb_queue_len(&sta->tx_filtered[ac]);
599
600	if (!has_buffered && !buffer_local)
601		return;
602
603	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
604		mpsp_trigger_send(sta, has_buffered, !buffer_local);
605	else
606		mps_frame_deliver(sta, 1);
607}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright 2012-2013, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de>
  4 * Copyright 2012-2013, cozybit Inc.
  5 * Copyright (C) 2021 Intel Corporation
  6 * Copyright (C) 2023 Intel Corporation
  7 */
  8
  9#include "mesh.h"
 10#include "wme.h"
 11
 12
 13/* mesh PS management */
 14
 15/**
 16 * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave
 17 * @sta: the station to get the frame for
 18 *
 19 * Returns: A newly allocated SKB
 20 */
 21static struct sk_buff *mps_qos_null_get(struct sta_info *sta)
 22{
 23	struct ieee80211_sub_if_data *sdata = sta->sdata;
 24	struct ieee80211_local *local = sdata->local;
 25	struct ieee80211_hdr *nullfunc; /* use 4addr header */
 26	struct sk_buff *skb;
 27	int size = sizeof(*nullfunc);
 28	__le16 fc;
 29
 30	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2);
 31	if (!skb)
 32		return NULL;
 33	skb_reserve(skb, local->hw.extra_tx_headroom);
 34
 35	nullfunc = skb_put(skb, size);
 36	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_NULLFUNC);
 37	ieee80211_fill_mesh_addresses(nullfunc, &fc, sta->sta.addr,
 38				      sdata->vif.addr);
 39	nullfunc->frame_control = fc;
 40	nullfunc->duration_id = 0;
 41	nullfunc->seq_ctrl = 0;
 42	/* no address resolution for this frame -> set addr 1 immediately */
 43	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
 44	skb_put_zero(skb, 2); /* append QoS control field */
 45	ieee80211_mps_set_frame_flags(sdata, sta, nullfunc);
 46
 47	return skb;
 48}
 49
 50/**
 51 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
 52 * @sta: the station to send to
 53 */
 54static void mps_qos_null_tx(struct sta_info *sta)
 55{
 56	struct sk_buff *skb;
 57
 58	skb = mps_qos_null_get(sta);
 59	if (!skb)
 60		return;
 61
 62	mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
 63		sta->sta.addr);
 64
 65	/* don't unintentionally start a MPSP */
 66	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
 67		u8 *qc = ieee80211_get_qos_ctl((void *) skb->data);
 68
 69		qc[0] |= IEEE80211_QOS_CTL_EOSP;
 70	}
 71
 72	ieee80211_tx_skb(sta->sdata, skb);
 73}
 74
 75/**
 76 * ieee80211_mps_local_status_update - track status of local link-specific PMs
 77 *
 78 * @sdata: local mesh subif
 79 *
 80 * sets the non-peer power mode and triggers the driver PS (re-)configuration
 81 * Return BSS_CHANGED_BEACON if a beacon update is necessary.
 82 *
 83 * Returns: BSS_CHANGED_BEACON if a beacon update is in order.
 84 */
 85u64 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
 86{
 87	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
 88	struct sta_info *sta;
 89	bool peering = false;
 90	int light_sleep_cnt = 0;
 91	int deep_sleep_cnt = 0;
 92	u64 changed = 0;
 93	enum nl80211_mesh_power_mode nonpeer_pm;
 94
 95	rcu_read_lock();
 96	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
 97		if (sdata != sta->sdata)
 98			continue;
 99
100		switch (sta->mesh->plink_state) {
101		case NL80211_PLINK_OPN_SNT:
102		case NL80211_PLINK_OPN_RCVD:
103		case NL80211_PLINK_CNF_RCVD:
104			peering = true;
105			break;
106		case NL80211_PLINK_ESTAB:
107			if (sta->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
108				light_sleep_cnt++;
109			else if (sta->mesh->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
110				deep_sleep_cnt++;
111			break;
112		default:
113			break;
114		}
115	}
116	rcu_read_unlock();
117
118	/*
119	 * Set non-peer mode to active during peering/scanning/authentication
120	 * (see IEEE802.11-2012 13.14.8.3). The non-peer mesh power mode is
121	 * deep sleep if the local STA is in light or deep sleep towards at
122	 * least one mesh peer (see 13.14.3.1). Otherwise, set it to the
123	 * user-configured default value.
124	 */
125	if (peering) {
126		mps_dbg(sdata, "setting non-peer PM to active for peering\n");
127		nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
128	} else if (light_sleep_cnt || deep_sleep_cnt) {
129		mps_dbg(sdata, "setting non-peer PM to deep sleep\n");
130		nonpeer_pm = NL80211_MESH_POWER_DEEP_SLEEP;
131	} else {
132		mps_dbg(sdata, "setting non-peer PM to user value\n");
133		nonpeer_pm = ifmsh->mshcfg.power_mode;
134	}
135
136	/* need update if sleep counts move between 0 and non-zero */
137	if (ifmsh->nonpeer_pm != nonpeer_pm ||
138	    !ifmsh->ps_peers_light_sleep != !light_sleep_cnt ||
139	    !ifmsh->ps_peers_deep_sleep != !deep_sleep_cnt)
140		changed = BSS_CHANGED_BEACON;
141
142	ifmsh->nonpeer_pm = nonpeer_pm;
143	ifmsh->ps_peers_light_sleep = light_sleep_cnt;
144	ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
145
146	return changed;
147}
148
149/**
150 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
151 *
152 * @sta: mesh STA
153 * @pm: the power mode to set
154 * Returns: BSS_CHANGED_BEACON if a beacon update is in order.
155 */
156u64 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
157				   enum nl80211_mesh_power_mode pm)
158{
159	struct ieee80211_sub_if_data *sdata = sta->sdata;
160
161	if (sta->mesh->local_pm == pm)
162		return 0;
163
164	mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
165		pm, sta->sta.addr);
166
167	sta->mesh->local_pm = pm;
168
169	/*
170	 * announce peer-specific power mode transition
171	 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
172	 */
173	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
174		mps_qos_null_tx(sta);
175
176	return ieee80211_mps_local_status_update(sdata);
177}
178
179/**
180 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
181 *
182 * @sdata: local mesh subif
183 * @sta: mesh STA
184 * @hdr: 802.11 frame header
185 *
186 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
187 *
188 * NOTE: sta must be given when an individually-addressed QoS frame header
189 * is handled, for group-addressed and management frames it is not used
190 */
191void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
192				   struct sta_info *sta,
193				   struct ieee80211_hdr *hdr)
194{
195	enum nl80211_mesh_power_mode pm;
196	u8 *qc;
197
198	if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
199		    ieee80211_is_data_qos(hdr->frame_control) &&
200		    !sta))
201		return;
202
203	if (is_unicast_ether_addr(hdr->addr1) &&
204	    ieee80211_is_data_qos(hdr->frame_control) &&
205	    sta->mesh->plink_state == NL80211_PLINK_ESTAB)
206		pm = sta->mesh->local_pm;
207	else
208		pm = sdata->u.mesh.nonpeer_pm;
209
210	if (pm == NL80211_MESH_POWER_ACTIVE)
211		hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
212	else
213		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
214
215	if (!ieee80211_is_data_qos(hdr->frame_control))
216		return;
217
218	qc = ieee80211_get_qos_ctl(hdr);
219
220	if ((is_unicast_ether_addr(hdr->addr1) &&
221	     pm == NL80211_MESH_POWER_DEEP_SLEEP) ||
222	    (is_multicast_ether_addr(hdr->addr1) &&
223	     sdata->u.mesh.ps_peers_deep_sleep > 0))
224		qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
225	else
226		qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
227}
228
229/**
230 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
231 *
232 * @sta: mesh STA
233 *
234 * called after change of peering status or non-peer/peer-specific power mode
235 */
236void ieee80211_mps_sta_status_update(struct sta_info *sta)
237{
238	enum nl80211_mesh_power_mode pm;
239	bool do_buffer;
240
241	/* For non-assoc STA, prevent buffering or frame transmission */
242	if (sta->sta_state < IEEE80211_STA_ASSOC)
243		return;
244
245	/*
246	 * use peer-specific power mode if peering is established and the
247	 * peer's power mode is known
248	 */
249	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
250	    sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN)
251		pm = sta->mesh->peer_pm;
252	else
253		pm = sta->mesh->nonpeer_pm;
254
255	do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
256
257	/* clear the MPSP flags for non-peers or active STA */
258	if (sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
259		clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
260		clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
261	} else if (!do_buffer) {
262		clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
263	}
264
265	/* Don't let the same PS state be set twice */
266	if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
267		return;
268
269	if (do_buffer) {
270		set_sta_flag(sta, WLAN_STA_PS_STA);
271		atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps);
272		mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n",
273			sta->sta.addr);
274	} else {
275		ieee80211_sta_ps_deliver_wakeup(sta);
276	}
277}
278
279static void mps_set_sta_peer_pm(struct sta_info *sta,
280				struct ieee80211_hdr *hdr)
281{
282	enum nl80211_mesh_power_mode pm;
283	u8 *qc = ieee80211_get_qos_ctl(hdr);
284
285	/*
286	 * Test Power Management field of frame control (PW) and
287	 * mesh power save level subfield of QoS control field (PSL)
288	 *
289	 * | PM | PSL| Mesh PM |
290	 * +----+----+---------+
291	 * | 0  |Rsrv|  Active |
292	 * | 1  | 0  |  Light  |
293	 * | 1  | 1  |  Deep   |
294	 */
295	if (ieee80211_has_pm(hdr->frame_control)) {
296		if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8))
297			pm = NL80211_MESH_POWER_DEEP_SLEEP;
298		else
299			pm = NL80211_MESH_POWER_LIGHT_SLEEP;
300	} else {
301		pm = NL80211_MESH_POWER_ACTIVE;
302	}
303
304	if (sta->mesh->peer_pm == pm)
305		return;
306
307	mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
308		sta->sta.addr, pm);
309
310	sta->mesh->peer_pm = pm;
311
312	ieee80211_mps_sta_status_update(sta);
313}
314
315static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
316				   struct ieee80211_hdr *hdr)
317{
318	enum nl80211_mesh_power_mode pm;
319
320	if (ieee80211_has_pm(hdr->frame_control))
321		pm = NL80211_MESH_POWER_DEEP_SLEEP;
322	else
323		pm = NL80211_MESH_POWER_ACTIVE;
324
325	if (sta->mesh->nonpeer_pm == pm)
326		return;
327
328	mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
329		sta->sta.addr, pm);
330
331	sta->mesh->nonpeer_pm = pm;
332
333	ieee80211_mps_sta_status_update(sta);
334}
335
336/**
337 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
338 *
339 * @sta: STA info that transmitted the frame
340 * @hdr: IEEE 802.11 (QoS) Header
341 */
342void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
343				    struct ieee80211_hdr *hdr)
344{
345	if (is_unicast_ether_addr(hdr->addr1) &&
346	    ieee80211_is_data_qos(hdr->frame_control)) {
347		/*
348		 * individually addressed QoS Data/Null frames contain
349		 * peer link-specific PS mode towards the local STA
350		 */
351		mps_set_sta_peer_pm(sta, hdr);
352
353		/* check for mesh Peer Service Period trigger frames */
354		ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
355					       sta, false, false);
356	} else {
357		/*
358		 * can only determine non-peer PS mode
359		 * (see IEEE802.11-2012 8.2.4.1.7)
360		 */
361		mps_set_sta_nonpeer_pm(sta, hdr);
362	}
363}
364
365
366/* mesh PS frame release */
367
368static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
369{
370	struct ieee80211_sub_if_data *sdata = sta->sdata;
371	struct sk_buff *skb;
372	struct ieee80211_hdr *nullfunc;
373	struct ieee80211_tx_info *info;
374	u8 *qc;
375
376	skb = mps_qos_null_get(sta);
377	if (!skb)
378		return;
379
380	nullfunc = (struct ieee80211_hdr *) skb->data;
381	if (!eosp)
382		nullfunc->frame_control |=
383				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
384	/*
385	 * | RSPI | EOSP |  MPSP triggering   |
386	 * +------+------+--------------------+
387	 * |  0   |  0   | local STA is owner |
388	 * |  0   |  1   | no MPSP (MPSP end) |
389	 * |  1   |  0   | both STA are owner |
390	 * |  1   |  1   | peer STA is owner  | see IEEE802.11-2012 13.14.9.2
391	 */
392	qc = ieee80211_get_qos_ctl(nullfunc);
393	if (rspi)
394		qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
395	if (eosp)
396		qc[0] |= IEEE80211_QOS_CTL_EOSP;
397
398	info = IEEE80211_SKB_CB(skb);
399
400	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
401		       IEEE80211_TX_CTL_REQ_TX_STATUS;
402
403	mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
404		rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
405
406	ieee80211_tx_skb(sdata, skb);
407}
408
409/**
410 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
411 * @sta: the station to handle
412 * @frames: the frame list to append to
413 *
414 * To properly end a mesh MPSP the last transmitted frame has to set the EOSP
415 * flag in the QoS Control field. In case the current tailing frame is not a
416 * QoS Data frame, append a QoS Null to carry the flag.
417 */
418static void mpsp_qos_null_append(struct sta_info *sta,
419				 struct sk_buff_head *frames)
420{
421	struct ieee80211_sub_if_data *sdata = sta->sdata;
422	struct sk_buff *new_skb, *skb = skb_peek_tail(frames);
423	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
424	struct ieee80211_tx_info *info;
425
426	if (ieee80211_is_data_qos(hdr->frame_control))
427		return;
428
429	new_skb = mps_qos_null_get(sta);
430	if (!new_skb)
431		return;
432
433	mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
434		sta->sta.addr);
435	/*
436	 * This frame has to be transmitted last. Assign lowest priority to
437	 * make sure it cannot pass other frames when releasing multiple ACs.
438	 */
439	new_skb->priority = 1;
440	skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
441	ieee80211_set_qos_hdr(sdata, new_skb);
442
443	info = IEEE80211_SKB_CB(new_skb);
444	info->control.vif = &sdata->vif;
445	info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
446
447	__skb_queue_tail(frames, new_skb);
448}
449
450/**
451 * mps_frame_deliver - transmit frames during mesh powersave
452 *
453 * @sta: STA info to transmit to
454 * @n_frames: number of frames to transmit. -1 for all
455 */
456static void mps_frame_deliver(struct sta_info *sta, int n_frames)
457{
458	struct ieee80211_local *local = sta->sdata->local;
459	int ac;
460	struct sk_buff_head frames;
461	struct sk_buff *skb;
462	bool more_data = false;
463
464	skb_queue_head_init(&frames);
465
466	/* collect frame(s) from buffers */
467	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
468		while (n_frames != 0) {
469			skb = skb_dequeue(&sta->tx_filtered[ac]);
470			if (!skb) {
471				skb = skb_dequeue(
472					&sta->ps_tx_buf[ac]);
473				if (skb)
474					local->total_ps_buffered--;
475			}
476			if (!skb)
477				break;
478			n_frames--;
479			__skb_queue_tail(&frames, skb);
480		}
481
482		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
483		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
484			more_data = true;
485	}
486
487	/* nothing to send? -> EOSP */
488	if (skb_queue_empty(&frames)) {
489		mpsp_trigger_send(sta, false, true);
490		return;
491	}
492
493	/* in a MPSP make sure the last skb is a QoS Data frame */
494	if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
495		mpsp_qos_null_append(sta, &frames);
496
497	mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
498		skb_queue_len(&frames), sta->sta.addr);
499
500	/* prepare collected frames for transmission */
501	skb_queue_walk(&frames, skb) {
502		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
503		struct ieee80211_hdr *hdr = (void *) skb->data;
504
505		/*
506		 * Tell TX path to send this frame even though the
507		 * STA may still remain is PS mode after this frame
508		 * exchange.
509		 */
510		info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
511
512		if (more_data || !skb_queue_is_last(&frames, skb))
513			hdr->frame_control |=
514				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
515		else
516			hdr->frame_control &=
517				cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
518
519		if (skb_queue_is_last(&frames, skb) &&
520		    ieee80211_is_data_qos(hdr->frame_control)) {
521			u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
522
523			/* MPSP trigger frame ends service period */
524			*qoshdr |= IEEE80211_QOS_CTL_EOSP;
525			info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
526		}
527	}
528
529	ieee80211_add_pending_skbs(local, &frames);
530	sta_info_recalc_tim(sta);
531}
532
533/**
534 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
535 *
536 * @qc: QoS Control field
537 * @sta: peer to start a MPSP with
538 * @tx: frame was transmitted by the local STA
539 * @acked: frame has been transmitted successfully
540 *
541 * NOTE: active mode STA may only serve as MPSP owner
542 */
543void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
544				    bool tx, bool acked)
545{
546	u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
547	u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
548
549	if (tx) {
550		if (rspi && acked)
551			set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
552
553		if (eosp)
554			clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
555		else if (acked &&
556			 test_sta_flag(sta, WLAN_STA_PS_STA) &&
557			 !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
558			mps_frame_deliver(sta, -1);
559	} else {
560		if (eosp)
561			clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
562		else if (sta->mesh->local_pm != NL80211_MESH_POWER_ACTIVE)
563			set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
564
565		if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
566			mps_frame_deliver(sta, -1);
567	}
568}
569
570/**
571 * ieee80211_mps_frame_release - release frames buffered due to mesh power save
572 *
573 * @sta: mesh STA
574 * @elems: IEs of beacon or probe response
575 *
576 * For peers if we have individually-addressed frames buffered or the peer
577 * indicates buffered frames, send a corresponding MPSP trigger frame. Since
578 * we do not evaluate the awake window duration, QoS Nulls are used as MPSP
579 * trigger frames. If the neighbour STA is not a peer, only send single frames.
580 */
581void ieee80211_mps_frame_release(struct sta_info *sta,
582				 struct ieee802_11_elems *elems)
583{
584	int ac, buffer_local = 0;
585	bool has_buffered = false;
586
587	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
588		has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
589						   sta->mesh->aid);
590
591	if (has_buffered)
592		mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
593			sta->sta.addr);
594
595	/* only transmit to PS STA with announced, non-zero awake window */
596	if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
597	    (!elems->awake_window || !get_unaligned_le16(elems->awake_window)))
598		return;
599
600	if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
601		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
602			buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
603					skb_queue_len(&sta->tx_filtered[ac]);
604
605	if (!has_buffered && !buffer_local)
606		return;
607
608	if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
609		mpsp_trigger_send(sta, has_buffered, !buffer_local);
610	else
611		mps_frame_deliver(sta, 1);
612}