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