1 // SPDX-License-Identifier: GPL-2.0-only
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
13 /* mesh PS management */
16 * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave
17 * @sta: the station to get the frame for
19 static struct sk_buff *mps_qos_null_get(struct sta_info *sta)
21 struct ieee80211_sub_if_data *sdata = sta->sdata;
22 struct ieee80211_local *local = sdata->local;
23 struct ieee80211_hdr *nullfunc; /* use 4addr header */
25 int size = sizeof(*nullfunc);
28 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2);
31 skb_reserve(skb, local->hw.extra_tx_headroom);
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,
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);
49 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
50 * @sta: the station to send to
52 static void mps_qos_null_tx(struct sta_info *sta)
56 skb = mps_qos_null_get(sta);
60 mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
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);
67 qc[0] |= IEEE80211_QOS_CTL_EOSP;
70 ieee80211_tx_skb(sta->sdata, skb);
74 * ieee80211_mps_local_status_update - track status of local link-specific PMs
76 * @sdata: local mesh subif
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.
81 u64 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
83 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
86 int light_sleep_cnt = 0;
87 int deep_sleep_cnt = 0;
89 enum nl80211_mesh_power_mode nonpeer_pm;
92 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
93 if (sdata != sta->sdata)
96 switch (sta->mesh->plink_state) {
97 case NL80211_PLINK_OPN_SNT:
98 case NL80211_PLINK_OPN_RCVD:
99 case NL80211_PLINK_CNF_RCVD:
102 case NL80211_PLINK_ESTAB:
103 if (sta->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
105 else if (sta->mesh->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
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.
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;
128 mps_dbg(sdata, "setting non-peer PM to user value\n");
129 nonpeer_pm = ifmsh->mshcfg.power_mode;
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;
138 ifmsh->nonpeer_pm = nonpeer_pm;
139 ifmsh->ps_peers_light_sleep = light_sleep_cnt;
140 ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
146 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
149 * @pm: the power mode to set
150 * Return BSS_CHANGED_BEACON if a beacon update is in order.
152 u64 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
153 enum nl80211_mesh_power_mode pm)
155 struct ieee80211_sub_if_data *sdata = sta->sdata;
157 if (sta->mesh->local_pm == pm)
160 mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
163 sta->mesh->local_pm = pm;
166 * announce peer-specific power mode transition
167 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
169 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
170 mps_qos_null_tx(sta);
172 return ieee80211_mps_local_status_update(sdata);
176 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
178 * @sdata: local mesh subif
180 * @hdr: 802.11 frame header
182 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
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
187 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
188 struct sta_info *sta,
189 struct ieee80211_hdr *hdr)
191 enum nl80211_mesh_power_mode pm;
194 if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
195 ieee80211_is_data_qos(hdr->frame_control) &&
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;
204 pm = sdata->u.mesh.nonpeer_pm;
206 if (pm == NL80211_MESH_POWER_ACTIVE)
207 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
209 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
211 if (!ieee80211_is_data_qos(hdr->frame_control))
214 qc = ieee80211_get_qos_ctl(hdr);
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);
222 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
226 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
230 * called after change of peering status or non-peer/peer-specific power mode
232 void ieee80211_mps_sta_status_update(struct sta_info *sta)
234 enum nl80211_mesh_power_mode pm;
237 /* For non-assoc STA, prevent buffering or frame transmission */
238 if (sta->sta_state < IEEE80211_STA_ASSOC)
242 * use peer-specific power mode if peering is established and the
243 * peer's power mode is known
245 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
246 sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN)
247 pm = sta->mesh->peer_pm;
249 pm = sta->mesh->nonpeer_pm;
251 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
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);
261 /* Don't let the same PS state be set twice */
262 if (test_sta_flag(sta, WLAN_STA_PS_STA) == 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",
271 ieee80211_sta_ps_deliver_wakeup(sta);
275 static void mps_set_sta_peer_pm(struct sta_info *sta,
276 struct ieee80211_hdr *hdr)
278 enum nl80211_mesh_power_mode pm;
279 u8 *qc = ieee80211_get_qos_ctl(hdr);
282 * Test Power Management field of frame control (PW) and
283 * mesh power save level subfield of QoS control field (PSL)
285 * | PM | PSL| Mesh PM |
286 * +----+----+---------+
287 * | 0 |Rsrv| Active |
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;
295 pm = NL80211_MESH_POWER_LIGHT_SLEEP;
297 pm = NL80211_MESH_POWER_ACTIVE;
300 if (sta->mesh->peer_pm == pm)
303 mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
306 sta->mesh->peer_pm = pm;
308 ieee80211_mps_sta_status_update(sta);
311 static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
312 struct ieee80211_hdr *hdr)
314 enum nl80211_mesh_power_mode pm;
316 if (ieee80211_has_pm(hdr->frame_control))
317 pm = NL80211_MESH_POWER_DEEP_SLEEP;
319 pm = NL80211_MESH_POWER_ACTIVE;
321 if (sta->mesh->nonpeer_pm == pm)
324 mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
327 sta->mesh->nonpeer_pm = pm;
329 ieee80211_mps_sta_status_update(sta);
333 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
335 * @sta: STA info that transmitted the frame
336 * @hdr: IEEE 802.11 (QoS) Header
338 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
339 struct ieee80211_hdr *hdr)
341 if (is_unicast_ether_addr(hdr->addr1) &&
342 ieee80211_is_data_qos(hdr->frame_control)) {
344 * individually addressed QoS Data/Null frames contain
345 * peer link-specific PS mode towards the local STA
347 mps_set_sta_peer_pm(sta, hdr);
349 /* check for mesh Peer Service Period trigger frames */
350 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
354 * can only determine non-peer PS mode
355 * (see IEEE802.11-2012 8.2.4.1.7)
357 mps_set_sta_nonpeer_pm(sta, hdr);
362 /* mesh PS frame release */
364 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
366 struct ieee80211_sub_if_data *sdata = sta->sdata;
368 struct ieee80211_hdr *nullfunc;
369 struct ieee80211_tx_info *info;
372 skb = mps_qos_null_get(sta);
376 nullfunc = (struct ieee80211_hdr *) skb->data;
378 nullfunc->frame_control |=
379 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
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
388 qc = ieee80211_get_qos_ctl(nullfunc);
390 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
392 qc[0] |= IEEE80211_QOS_CTL_EOSP;
394 info = IEEE80211_SKB_CB(skb);
396 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
397 IEEE80211_TX_CTL_REQ_TX_STATUS;
399 mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
400 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
402 ieee80211_tx_skb(sdata, skb);
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
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.
414 static void mpsp_qos_null_append(struct sta_info *sta,
415 struct sk_buff_head *frames)
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;
422 if (ieee80211_is_data_qos(hdr->frame_control))
425 new_skb = mps_qos_null_get(sta);
429 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
432 * This frame has to be transmitted last. Assign lowest priority to
433 * make sure it cannot pass other frames when releasing multiple ACs.
435 new_skb->priority = 1;
436 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
437 ieee80211_set_qos_hdr(sdata, new_skb);
439 info = IEEE80211_SKB_CB(new_skb);
440 info->control.vif = &sdata->vif;
441 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
443 __skb_queue_tail(frames, new_skb);
447 * mps_frame_deliver - transmit frames during mesh powersave
449 * @sta: STA info to transmit to
450 * @n_frames: number of frames to transmit. -1 for all
452 static void mps_frame_deliver(struct sta_info *sta, int n_frames)
454 struct ieee80211_local *local = sta->sdata->local;
456 struct sk_buff_head frames;
458 bool more_data = false;
460 skb_queue_head_init(&frames);
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]);
468 &sta->ps_tx_buf[ac]);
470 local->total_ps_buffered--;
475 __skb_queue_tail(&frames, skb);
478 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
479 !skb_queue_empty(&sta->ps_tx_buf[ac]))
483 /* nothing to send? -> EOSP */
484 if (skb_queue_empty(&frames)) {
485 mpsp_trigger_send(sta, false, true);
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);
493 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
494 skb_queue_len(&frames), sta->sta.addr);
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;
502 * Tell TX path to send this frame even though the
503 * STA may still remain is PS mode after this frame
506 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
508 if (more_data || !skb_queue_is_last(&frames, skb))
509 hdr->frame_control |=
510 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
512 hdr->frame_control &=
513 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
515 if (skb_queue_is_last(&frames, skb) &&
516 ieee80211_is_data_qos(hdr->frame_control)) {
517 u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
519 /* MPSP trigger frame ends service period */
520 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
521 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
525 ieee80211_add_pending_skbs(local, &frames);
526 sta_info_recalc_tim(sta);
530 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
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
537 * NOTE: active mode STA may only serve as MPSP owner
539 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
542 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
543 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
547 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
550 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
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);
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);
561 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
562 mps_frame_deliver(sta, -1);
567 * ieee80211_mps_frame_release - release frames buffered due to mesh power save
570 * @elems: IEs of beacon or probe response
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.
577 void ieee80211_mps_frame_release(struct sta_info *sta,
578 struct ieee802_11_elems *elems)
580 int ac, buffer_local = 0;
581 bool has_buffered = false;
583 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
584 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
588 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
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)))
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]);
601 if (!has_buffered && !buffer_local)
604 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
605 mpsp_trigger_send(sta, has_buffered, !buffer_local);
607 mps_frame_deliver(sta, 1);