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
12 /* mesh PS management */
15 * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave
17 static struct sk_buff *mps_qos_null_get(struct sta_info *sta)
19 struct ieee80211_sub_if_data *sdata = sta->sdata;
20 struct ieee80211_local *local = sdata->local;
21 struct ieee80211_hdr *nullfunc; /* use 4addr header */
23 int size = sizeof(*nullfunc);
26 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2);
29 skb_reserve(skb, local->hw.extra_tx_headroom);
31 nullfunc = skb_put(skb, size);
32 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_NULLFUNC);
33 ieee80211_fill_mesh_addresses(nullfunc, &fc, sta->sta.addr,
35 nullfunc->frame_control = fc;
36 nullfunc->duration_id = 0;
37 nullfunc->seq_ctrl = 0;
38 /* no address resolution for this frame -> set addr 1 immediately */
39 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
40 skb_put_zero(skb, 2); /* append QoS control field */
41 ieee80211_mps_set_frame_flags(sdata, sta, nullfunc);
47 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
49 static void mps_qos_null_tx(struct sta_info *sta)
53 skb = mps_qos_null_get(sta);
57 mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
60 /* don't unintentionally start a MPSP */
61 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
62 u8 *qc = ieee80211_get_qos_ctl((void *) skb->data);
64 qc[0] |= IEEE80211_QOS_CTL_EOSP;
67 ieee80211_tx_skb(sta->sdata, skb);
71 * ieee80211_mps_local_status_update - track status of local link-specific PMs
73 * @sdata: local mesh subif
75 * sets the non-peer power mode and triggers the driver PS (re-)configuration
76 * Return BSS_CHANGED_BEACON if a beacon update is necessary.
78 u32 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
80 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
83 int light_sleep_cnt = 0;
84 int deep_sleep_cnt = 0;
86 enum nl80211_mesh_power_mode nonpeer_pm;
89 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
90 if (sdata != sta->sdata)
93 switch (sta->mesh->plink_state) {
94 case NL80211_PLINK_OPN_SNT:
95 case NL80211_PLINK_OPN_RCVD:
96 case NL80211_PLINK_CNF_RCVD:
99 case NL80211_PLINK_ESTAB:
100 if (sta->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
102 else if (sta->mesh->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
112 * Set non-peer mode to active during peering/scanning/authentication
113 * (see IEEE802.11-2012 13.14.8.3). The non-peer mesh power mode is
114 * deep sleep if the local STA is in light or deep sleep towards at
115 * least one mesh peer (see 13.14.3.1). Otherwise, set it to the
116 * user-configured default value.
119 mps_dbg(sdata, "setting non-peer PM to active for peering\n");
120 nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
121 } else if (light_sleep_cnt || deep_sleep_cnt) {
122 mps_dbg(sdata, "setting non-peer PM to deep sleep\n");
123 nonpeer_pm = NL80211_MESH_POWER_DEEP_SLEEP;
125 mps_dbg(sdata, "setting non-peer PM to user value\n");
126 nonpeer_pm = ifmsh->mshcfg.power_mode;
129 /* need update if sleep counts move between 0 and non-zero */
130 if (ifmsh->nonpeer_pm != nonpeer_pm ||
131 !ifmsh->ps_peers_light_sleep != !light_sleep_cnt ||
132 !ifmsh->ps_peers_deep_sleep != !deep_sleep_cnt)
133 changed = BSS_CHANGED_BEACON;
135 ifmsh->nonpeer_pm = nonpeer_pm;
136 ifmsh->ps_peers_light_sleep = light_sleep_cnt;
137 ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
143 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
146 * @pm: the power mode to set
147 * Return BSS_CHANGED_BEACON if a beacon update is in order.
149 u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
150 enum nl80211_mesh_power_mode pm)
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
154 if (sta->mesh->local_pm == pm)
157 mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
160 sta->mesh->local_pm = pm;
163 * announce peer-specific power mode transition
164 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
166 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
167 mps_qos_null_tx(sta);
169 return ieee80211_mps_local_status_update(sdata);
173 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
175 * @sdata: local mesh subif
177 * @hdr: 802.11 frame header
179 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
181 * NOTE: sta must be given when an individually-addressed QoS frame header
182 * is handled, for group-addressed and management frames it is not used
184 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
185 struct sta_info *sta,
186 struct ieee80211_hdr *hdr)
188 enum nl80211_mesh_power_mode pm;
191 if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
192 ieee80211_is_data_qos(hdr->frame_control) &&
196 if (is_unicast_ether_addr(hdr->addr1) &&
197 ieee80211_is_data_qos(hdr->frame_control) &&
198 sta->mesh->plink_state == NL80211_PLINK_ESTAB)
199 pm = sta->mesh->local_pm;
201 pm = sdata->u.mesh.nonpeer_pm;
203 if (pm == NL80211_MESH_POWER_ACTIVE)
204 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
206 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
208 if (!ieee80211_is_data_qos(hdr->frame_control))
211 qc = ieee80211_get_qos_ctl(hdr);
213 if ((is_unicast_ether_addr(hdr->addr1) &&
214 pm == NL80211_MESH_POWER_DEEP_SLEEP) ||
215 (is_multicast_ether_addr(hdr->addr1) &&
216 sdata->u.mesh.ps_peers_deep_sleep > 0))
217 qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
219 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
223 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
227 * called after change of peering status or non-peer/peer-specific power mode
229 void ieee80211_mps_sta_status_update(struct sta_info *sta)
231 enum nl80211_mesh_power_mode pm;
234 /* For non-assoc STA, prevent buffering or frame transmission */
235 if (sta->sta_state < IEEE80211_STA_ASSOC)
239 * use peer-specific power mode if peering is established and the
240 * peer's power mode is known
242 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
243 sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN)
244 pm = sta->mesh->peer_pm;
246 pm = sta->mesh->nonpeer_pm;
248 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
250 /* clear the MPSP flags for non-peers or active STA */
251 if (sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
252 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
253 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
254 } else if (!do_buffer) {
255 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
258 /* Don't let the same PS state be set twice */
259 if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
263 set_sta_flag(sta, WLAN_STA_PS_STA);
264 atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps);
265 mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n",
268 ieee80211_sta_ps_deliver_wakeup(sta);
272 static void mps_set_sta_peer_pm(struct sta_info *sta,
273 struct ieee80211_hdr *hdr)
275 enum nl80211_mesh_power_mode pm;
276 u8 *qc = ieee80211_get_qos_ctl(hdr);
279 * Test Power Management field of frame control (PW) and
280 * mesh power save level subfield of QoS control field (PSL)
282 * | PM | PSL| Mesh PM |
283 * +----+----+---------+
284 * | 0 |Rsrv| Active |
288 if (ieee80211_has_pm(hdr->frame_control)) {
289 if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8))
290 pm = NL80211_MESH_POWER_DEEP_SLEEP;
292 pm = NL80211_MESH_POWER_LIGHT_SLEEP;
294 pm = NL80211_MESH_POWER_ACTIVE;
297 if (sta->mesh->peer_pm == pm)
300 mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
303 sta->mesh->peer_pm = pm;
305 ieee80211_mps_sta_status_update(sta);
308 static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
309 struct ieee80211_hdr *hdr)
311 enum nl80211_mesh_power_mode pm;
313 if (ieee80211_has_pm(hdr->frame_control))
314 pm = NL80211_MESH_POWER_DEEP_SLEEP;
316 pm = NL80211_MESH_POWER_ACTIVE;
318 if (sta->mesh->nonpeer_pm == pm)
321 mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
324 sta->mesh->nonpeer_pm = pm;
326 ieee80211_mps_sta_status_update(sta);
330 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
332 * @sta: STA info that transmitted the frame
333 * @hdr: IEEE 802.11 (QoS) Header
335 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
336 struct ieee80211_hdr *hdr)
338 if (is_unicast_ether_addr(hdr->addr1) &&
339 ieee80211_is_data_qos(hdr->frame_control)) {
341 * individually addressed QoS Data/Null frames contain
342 * peer link-specific PS mode towards the local STA
344 mps_set_sta_peer_pm(sta, hdr);
346 /* check for mesh Peer Service Period trigger frames */
347 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
351 * can only determine non-peer PS mode
352 * (see IEEE802.11-2012 8.2.4.1.7)
354 mps_set_sta_nonpeer_pm(sta, hdr);
359 /* mesh PS frame release */
361 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
363 struct ieee80211_sub_if_data *sdata = sta->sdata;
365 struct ieee80211_hdr *nullfunc;
366 struct ieee80211_tx_info *info;
369 skb = mps_qos_null_get(sta);
373 nullfunc = (struct ieee80211_hdr *) skb->data;
375 nullfunc->frame_control |=
376 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
378 * | RSPI | EOSP | MPSP triggering |
379 * +------+------+--------------------+
380 * | 0 | 0 | local STA is owner |
381 * | 0 | 1 | no MPSP (MPSP end) |
382 * | 1 | 0 | both STA are owner |
383 * | 1 | 1 | peer STA is owner | see IEEE802.11-2012 13.14.9.2
385 qc = ieee80211_get_qos_ctl(nullfunc);
387 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
389 qc[0] |= IEEE80211_QOS_CTL_EOSP;
391 info = IEEE80211_SKB_CB(skb);
393 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
394 IEEE80211_TX_CTL_REQ_TX_STATUS;
396 mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
397 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
399 ieee80211_tx_skb(sdata, skb);
403 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
405 * To properly end a mesh MPSP the last transmitted frame has to set the EOSP
406 * flag in the QoS Control field. In case the current tailing frame is not a
407 * QoS Data frame, append a QoS Null to carry the flag.
409 static void mpsp_qos_null_append(struct sta_info *sta,
410 struct sk_buff_head *frames)
412 struct ieee80211_sub_if_data *sdata = sta->sdata;
413 struct sk_buff *new_skb, *skb = skb_peek_tail(frames);
414 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
415 struct ieee80211_tx_info *info;
417 if (ieee80211_is_data_qos(hdr->frame_control))
420 new_skb = mps_qos_null_get(sta);
424 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
427 * This frame has to be transmitted last. Assign lowest priority to
428 * make sure it cannot pass other frames when releasing multiple ACs.
430 new_skb->priority = 1;
431 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
432 ieee80211_set_qos_hdr(sdata, new_skb);
434 info = IEEE80211_SKB_CB(new_skb);
435 info->control.vif = &sdata->vif;
436 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
438 __skb_queue_tail(frames, new_skb);
442 * mps_frame_deliver - transmit frames during mesh powersave
444 * @sta: STA info to transmit to
445 * @n_frames: number of frames to transmit. -1 for all
447 static void mps_frame_deliver(struct sta_info *sta, int n_frames)
449 struct ieee80211_local *local = sta->sdata->local;
451 struct sk_buff_head frames;
453 bool more_data = false;
455 skb_queue_head_init(&frames);
457 /* collect frame(s) from buffers */
458 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
459 while (n_frames != 0) {
460 skb = skb_dequeue(&sta->tx_filtered[ac]);
463 &sta->ps_tx_buf[ac]);
465 local->total_ps_buffered--;
470 __skb_queue_tail(&frames, skb);
473 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
474 !skb_queue_empty(&sta->ps_tx_buf[ac]))
478 /* nothing to send? -> EOSP */
479 if (skb_queue_empty(&frames)) {
480 mpsp_trigger_send(sta, false, true);
484 /* in a MPSP make sure the last skb is a QoS Data frame */
485 if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
486 mpsp_qos_null_append(sta, &frames);
488 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
489 skb_queue_len(&frames), sta->sta.addr);
491 /* prepare collected frames for transmission */
492 skb_queue_walk(&frames, skb) {
493 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
494 struct ieee80211_hdr *hdr = (void *) skb->data;
497 * Tell TX path to send this frame even though the
498 * STA may still remain is PS mode after this frame
501 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
503 if (more_data || !skb_queue_is_last(&frames, skb))
504 hdr->frame_control |=
505 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
507 hdr->frame_control &=
508 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
510 if (skb_queue_is_last(&frames, skb) &&
511 ieee80211_is_data_qos(hdr->frame_control)) {
512 u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
514 /* MPSP trigger frame ends service period */
515 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
516 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
520 ieee80211_add_pending_skbs(local, &frames);
521 sta_info_recalc_tim(sta);
525 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
527 * @qc: QoS Control field
528 * @sta: peer to start a MPSP with
529 * @tx: frame was transmitted by the local STA
530 * @acked: frame has been transmitted successfully
532 * NOTE: active mode STA may only serve as MPSP owner
534 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
537 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
538 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
542 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
545 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
547 test_sta_flag(sta, WLAN_STA_PS_STA) &&
548 !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
549 mps_frame_deliver(sta, -1);
552 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
553 else if (sta->mesh->local_pm != NL80211_MESH_POWER_ACTIVE)
554 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
556 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
557 mps_frame_deliver(sta, -1);
562 * ieee80211_mps_frame_release - release frames buffered due to mesh power save
565 * @elems: IEs of beacon or probe response
567 * For peers if we have individually-addressed frames buffered or the peer
568 * indicates buffered frames, send a corresponding MPSP trigger frame. Since
569 * we do not evaluate the awake window duration, QoS Nulls are used as MPSP
570 * trigger frames. If the neighbour STA is not a peer, only send single frames.
572 void ieee80211_mps_frame_release(struct sta_info *sta,
573 struct ieee802_11_elems *elems)
575 int ac, buffer_local = 0;
576 bool has_buffered = false;
578 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
579 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
583 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
586 /* only transmit to PS STA with announced, non-zero awake window */
587 if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
588 (!elems->awake_window || !get_unaligned_le16(elems->awake_window)))
591 if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
592 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
593 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
594 skb_queue_len(&sta->tx_filtered[ac]);
596 if (!has_buffered && !buffer_local)
599 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
600 mpsp_trigger_send(sta, has_buffered, !buffer_local);
602 mps_frame_deliver(sta, 1);