Merge tag 'iommu-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux/fpc-iii.git] / net / mac80211 / mesh_ps.c
blob204830a55240b4829991f4d815a6f1a10538c0f5
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 */
7 #include "mesh.h"
8 #include "wme.h"
11 /* mesh PS management */
13 /**
14 * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave
15 * @sta: the station to get the frame for
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 */
22 struct sk_buff *skb;
23 int size = sizeof(*nullfunc);
24 __le16 fc;
26 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2);
27 if (!skb)
28 return NULL;
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,
34 sdata->vif.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);
43 return skb;
46 /**
47 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
48 * @sta: the station to send to
50 static void mps_qos_null_tx(struct sta_info *sta)
52 struct sk_buff *skb;
54 skb = mps_qos_null_get(sta);
55 if (!skb)
56 return;
58 mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
59 sta->sta.addr);
61 /* don't unintentionally start a MPSP */
62 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
63 u8 *qc = ieee80211_get_qos_ctl((void *) skb->data);
65 qc[0] |= IEEE80211_QOS_CTL_EOSP;
68 ieee80211_tx_skb(sta->sdata, skb);
71 /**
72 * ieee80211_mps_local_status_update - track status of local link-specific PMs
74 * @sdata: local mesh subif
76 * sets the non-peer power mode and triggers the driver PS (re-)configuration
77 * Return BSS_CHANGED_BEACON if a beacon update is necessary.
79 u32 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
81 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
82 struct sta_info *sta;
83 bool peering = false;
84 int light_sleep_cnt = 0;
85 int deep_sleep_cnt = 0;
86 u32 changed = 0;
87 enum nl80211_mesh_power_mode nonpeer_pm;
89 rcu_read_lock();
90 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
91 if (sdata != sta->sdata)
92 continue;
94 switch (sta->mesh->plink_state) {
95 case NL80211_PLINK_OPN_SNT:
96 case NL80211_PLINK_OPN_RCVD:
97 case NL80211_PLINK_CNF_RCVD:
98 peering = true;
99 break;
100 case NL80211_PLINK_ESTAB:
101 if (sta->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
102 light_sleep_cnt++;
103 else if (sta->mesh->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
104 deep_sleep_cnt++;
105 break;
106 default:
107 break;
110 rcu_read_unlock();
113 * Set non-peer mode to active during peering/scanning/authentication
114 * (see IEEE802.11-2012 13.14.8.3). The non-peer mesh power mode is
115 * deep sleep if the local STA is in light or deep sleep towards at
116 * least one mesh peer (see 13.14.3.1). Otherwise, set it to the
117 * user-configured default value.
119 if (peering) {
120 mps_dbg(sdata, "setting non-peer PM to active for peering\n");
121 nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
122 } else if (light_sleep_cnt || deep_sleep_cnt) {
123 mps_dbg(sdata, "setting non-peer PM to deep sleep\n");
124 nonpeer_pm = NL80211_MESH_POWER_DEEP_SLEEP;
125 } else {
126 mps_dbg(sdata, "setting non-peer PM to user value\n");
127 nonpeer_pm = ifmsh->mshcfg.power_mode;
130 /* need update if sleep counts move between 0 and non-zero */
131 if (ifmsh->nonpeer_pm != nonpeer_pm ||
132 !ifmsh->ps_peers_light_sleep != !light_sleep_cnt ||
133 !ifmsh->ps_peers_deep_sleep != !deep_sleep_cnt)
134 changed = BSS_CHANGED_BEACON;
136 ifmsh->nonpeer_pm = nonpeer_pm;
137 ifmsh->ps_peers_light_sleep = light_sleep_cnt;
138 ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
140 return changed;
144 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
146 * @sta: mesh STA
147 * @pm: the power mode to set
148 * Return BSS_CHANGED_BEACON if a beacon update is in order.
150 u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
151 enum nl80211_mesh_power_mode pm)
153 struct ieee80211_sub_if_data *sdata = sta->sdata;
155 if (sta->mesh->local_pm == pm)
156 return 0;
158 mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
159 pm, sta->sta.addr);
161 sta->mesh->local_pm = pm;
164 * announce peer-specific power mode transition
165 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
167 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
168 mps_qos_null_tx(sta);
170 return ieee80211_mps_local_status_update(sdata);
174 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
176 * @sdata: local mesh subif
177 * @sta: mesh STA
178 * @hdr: 802.11 frame header
180 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
182 * NOTE: sta must be given when an individually-addressed QoS frame header
183 * is handled, for group-addressed and management frames it is not used
185 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
186 struct sta_info *sta,
187 struct ieee80211_hdr *hdr)
189 enum nl80211_mesh_power_mode pm;
190 u8 *qc;
192 if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
193 ieee80211_is_data_qos(hdr->frame_control) &&
194 !sta))
195 return;
197 if (is_unicast_ether_addr(hdr->addr1) &&
198 ieee80211_is_data_qos(hdr->frame_control) &&
199 sta->mesh->plink_state == NL80211_PLINK_ESTAB)
200 pm = sta->mesh->local_pm;
201 else
202 pm = sdata->u.mesh.nonpeer_pm;
204 if (pm == NL80211_MESH_POWER_ACTIVE)
205 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
206 else
207 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
209 if (!ieee80211_is_data_qos(hdr->frame_control))
210 return;
212 qc = ieee80211_get_qos_ctl(hdr);
214 if ((is_unicast_ether_addr(hdr->addr1) &&
215 pm == NL80211_MESH_POWER_DEEP_SLEEP) ||
216 (is_multicast_ether_addr(hdr->addr1) &&
217 sdata->u.mesh.ps_peers_deep_sleep > 0))
218 qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
219 else
220 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
224 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
226 * @sta: mesh STA
228 * called after change of peering status or non-peer/peer-specific power mode
230 void ieee80211_mps_sta_status_update(struct sta_info *sta)
232 enum nl80211_mesh_power_mode pm;
233 bool do_buffer;
235 /* For non-assoc STA, prevent buffering or frame transmission */
236 if (sta->sta_state < IEEE80211_STA_ASSOC)
237 return;
240 * use peer-specific power mode if peering is established and the
241 * peer's power mode is known
243 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
244 sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN)
245 pm = sta->mesh->peer_pm;
246 else
247 pm = sta->mesh->nonpeer_pm;
249 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
251 /* clear the MPSP flags for non-peers or active STA */
252 if (sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
253 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
254 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
255 } else if (!do_buffer) {
256 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
259 /* Don't let the same PS state be set twice */
260 if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
261 return;
263 if (do_buffer) {
264 set_sta_flag(sta, WLAN_STA_PS_STA);
265 atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps);
266 mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n",
267 sta->sta.addr);
268 } else {
269 ieee80211_sta_ps_deliver_wakeup(sta);
273 static void mps_set_sta_peer_pm(struct sta_info *sta,
274 struct ieee80211_hdr *hdr)
276 enum nl80211_mesh_power_mode pm;
277 u8 *qc = ieee80211_get_qos_ctl(hdr);
280 * Test Power Management field of frame control (PW) and
281 * mesh power save level subfield of QoS control field (PSL)
283 * | PM | PSL| Mesh PM |
284 * +----+----+---------+
285 * | 0 |Rsrv| Active |
286 * | 1 | 0 | Light |
287 * | 1 | 1 | Deep |
289 if (ieee80211_has_pm(hdr->frame_control)) {
290 if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8))
291 pm = NL80211_MESH_POWER_DEEP_SLEEP;
292 else
293 pm = NL80211_MESH_POWER_LIGHT_SLEEP;
294 } else {
295 pm = NL80211_MESH_POWER_ACTIVE;
298 if (sta->mesh->peer_pm == pm)
299 return;
301 mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
302 sta->sta.addr, pm);
304 sta->mesh->peer_pm = pm;
306 ieee80211_mps_sta_status_update(sta);
309 static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
310 struct ieee80211_hdr *hdr)
312 enum nl80211_mesh_power_mode pm;
314 if (ieee80211_has_pm(hdr->frame_control))
315 pm = NL80211_MESH_POWER_DEEP_SLEEP;
316 else
317 pm = NL80211_MESH_POWER_ACTIVE;
319 if (sta->mesh->nonpeer_pm == pm)
320 return;
322 mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
323 sta->sta.addr, pm);
325 sta->mesh->nonpeer_pm = pm;
327 ieee80211_mps_sta_status_update(sta);
331 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
333 * @sta: STA info that transmitted the frame
334 * @hdr: IEEE 802.11 (QoS) Header
336 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
337 struct ieee80211_hdr *hdr)
339 if (is_unicast_ether_addr(hdr->addr1) &&
340 ieee80211_is_data_qos(hdr->frame_control)) {
342 * individually addressed QoS Data/Null frames contain
343 * peer link-specific PS mode towards the local STA
345 mps_set_sta_peer_pm(sta, hdr);
347 /* check for mesh Peer Service Period trigger frames */
348 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
349 sta, false, false);
350 } else {
352 * can only determine non-peer PS mode
353 * (see IEEE802.11-2012 8.2.4.1.7)
355 mps_set_sta_nonpeer_pm(sta, hdr);
360 /* mesh PS frame release */
362 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
364 struct ieee80211_sub_if_data *sdata = sta->sdata;
365 struct sk_buff *skb;
366 struct ieee80211_hdr *nullfunc;
367 struct ieee80211_tx_info *info;
368 u8 *qc;
370 skb = mps_qos_null_get(sta);
371 if (!skb)
372 return;
374 nullfunc = (struct ieee80211_hdr *) skb->data;
375 if (!eosp)
376 nullfunc->frame_control |=
377 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
379 * | RSPI | EOSP | MPSP triggering |
380 * +------+------+--------------------+
381 * | 0 | 0 | local STA is owner |
382 * | 0 | 1 | no MPSP (MPSP end) |
383 * | 1 | 0 | both STA are owner |
384 * | 1 | 1 | peer STA is owner | see IEEE802.11-2012 13.14.9.2
386 qc = ieee80211_get_qos_ctl(nullfunc);
387 if (rspi)
388 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
389 if (eosp)
390 qc[0] |= IEEE80211_QOS_CTL_EOSP;
392 info = IEEE80211_SKB_CB(skb);
394 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
395 IEEE80211_TX_CTL_REQ_TX_STATUS;
397 mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
398 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
400 ieee80211_tx_skb(sdata, skb);
404 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
405 * @sta: the station to handle
406 * @frames: the frame list to append to
408 * To properly end a mesh MPSP the last transmitted frame has to set the EOSP
409 * flag in the QoS Control field. In case the current tailing frame is not a
410 * QoS Data frame, append a QoS Null to carry the flag.
412 static void mpsp_qos_null_append(struct sta_info *sta,
413 struct sk_buff_head *frames)
415 struct ieee80211_sub_if_data *sdata = sta->sdata;
416 struct sk_buff *new_skb, *skb = skb_peek_tail(frames);
417 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
418 struct ieee80211_tx_info *info;
420 if (ieee80211_is_data_qos(hdr->frame_control))
421 return;
423 new_skb = mps_qos_null_get(sta);
424 if (!new_skb)
425 return;
427 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
428 sta->sta.addr);
430 * This frame has to be transmitted last. Assign lowest priority to
431 * make sure it cannot pass other frames when releasing multiple ACs.
433 new_skb->priority = 1;
434 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
435 ieee80211_set_qos_hdr(sdata, new_skb);
437 info = IEEE80211_SKB_CB(new_skb);
438 info->control.vif = &sdata->vif;
439 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
441 __skb_queue_tail(frames, new_skb);
445 * mps_frame_deliver - transmit frames during mesh powersave
447 * @sta: STA info to transmit to
448 * @n_frames: number of frames to transmit. -1 for all
450 static void mps_frame_deliver(struct sta_info *sta, int n_frames)
452 struct ieee80211_local *local = sta->sdata->local;
453 int ac;
454 struct sk_buff_head frames;
455 struct sk_buff *skb;
456 bool more_data = false;
458 skb_queue_head_init(&frames);
460 /* collect frame(s) from buffers */
461 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
462 while (n_frames != 0) {
463 skb = skb_dequeue(&sta->tx_filtered[ac]);
464 if (!skb) {
465 skb = skb_dequeue(
466 &sta->ps_tx_buf[ac]);
467 if (skb)
468 local->total_ps_buffered--;
470 if (!skb)
471 break;
472 n_frames--;
473 __skb_queue_tail(&frames, skb);
476 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
477 !skb_queue_empty(&sta->ps_tx_buf[ac]))
478 more_data = true;
481 /* nothing to send? -> EOSP */
482 if (skb_queue_empty(&frames)) {
483 mpsp_trigger_send(sta, false, true);
484 return;
487 /* in a MPSP make sure the last skb is a QoS Data frame */
488 if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
489 mpsp_qos_null_append(sta, &frames);
491 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
492 skb_queue_len(&frames), sta->sta.addr);
494 /* prepare collected frames for transmission */
495 skb_queue_walk(&frames, skb) {
496 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
497 struct ieee80211_hdr *hdr = (void *) skb->data;
500 * Tell TX path to send this frame even though the
501 * STA may still remain is PS mode after this frame
502 * exchange.
504 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
506 if (more_data || !skb_queue_is_last(&frames, skb))
507 hdr->frame_control |=
508 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
509 else
510 hdr->frame_control &=
511 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
513 if (skb_queue_is_last(&frames, skb) &&
514 ieee80211_is_data_qos(hdr->frame_control)) {
515 u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
517 /* MPSP trigger frame ends service period */
518 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
519 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
523 ieee80211_add_pending_skbs(local, &frames);
524 sta_info_recalc_tim(sta);
528 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
530 * @qc: QoS Control field
531 * @sta: peer to start a MPSP with
532 * @tx: frame was transmitted by the local STA
533 * @acked: frame has been transmitted successfully
535 * NOTE: active mode STA may only serve as MPSP owner
537 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
538 bool tx, bool acked)
540 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
541 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
543 if (tx) {
544 if (rspi && acked)
545 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
547 if (eosp)
548 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
549 else if (acked &&
550 test_sta_flag(sta, WLAN_STA_PS_STA) &&
551 !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
552 mps_frame_deliver(sta, -1);
553 } else {
554 if (eosp)
555 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
556 else if (sta->mesh->local_pm != NL80211_MESH_POWER_ACTIVE)
557 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
559 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
560 mps_frame_deliver(sta, -1);
565 * ieee80211_mps_frame_release - release frames buffered due to mesh power save
567 * @sta: mesh STA
568 * @elems: IEs of beacon or probe response
570 * For peers if we have individually-addressed frames buffered or the peer
571 * indicates buffered frames, send a corresponding MPSP trigger frame. Since
572 * we do not evaluate the awake window duration, QoS Nulls are used as MPSP
573 * trigger frames. If the neighbour STA is not a peer, only send single frames.
575 void ieee80211_mps_frame_release(struct sta_info *sta,
576 struct ieee802_11_elems *elems)
578 int ac, buffer_local = 0;
579 bool has_buffered = false;
581 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
582 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
583 sta->mesh->aid);
585 if (has_buffered)
586 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
587 sta->sta.addr);
589 /* only transmit to PS STA with announced, non-zero awake window */
590 if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
591 (!elems->awake_window || !le16_to_cpu(*elems->awake_window)))
592 return;
594 if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
595 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
596 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
597 skb_queue_len(&sta->tx_filtered[ac]);
599 if (!has_buffered && !buffer_local)
600 return;
602 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
603 mpsp_trigger_send(sta, has_buffered, !buffer_local);
604 else
605 mps_frame_deliver(sta, 1);