GNU Linux-libre 5.4.207-gnu1
[releases.git] / net / mac80211 / mesh_ps.c
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  */
17 static struct sk_buff *mps_qos_null_get(struct sta_info *sta)
18 {
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;
25
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);
30
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);
42
43         return skb;
44 }
45
46 /**
47  * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
48  */
49 static void mps_qos_null_tx(struct sta_info *sta)
50 {
51         struct sk_buff *skb;
52
53         skb = mps_qos_null_get(sta);
54         if (!skb)
55                 return;
56
57         mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
58                 sta->sta.addr);
59
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);
63
64                 qc[0] |= IEEE80211_QOS_CTL_EOSP;
65         }
66
67         ieee80211_tx_skb(sta->sdata, skb);
68 }
69
70 /**
71  * ieee80211_mps_local_status_update - track status of local link-specific PMs
72  *
73  * @sdata: local mesh subif
74  *
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.
77  */
78 u32 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
79 {
80         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
81         struct sta_info *sta;
82         bool peering = false;
83         int light_sleep_cnt = 0;
84         int deep_sleep_cnt = 0;
85         u32 changed = 0;
86         enum nl80211_mesh_power_mode nonpeer_pm;
87
88         rcu_read_lock();
89         list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
90                 if (sdata != sta->sdata)
91                         continue;
92
93                 switch (sta->mesh->plink_state) {
94                 case NL80211_PLINK_OPN_SNT:
95                 case NL80211_PLINK_OPN_RCVD:
96                 case NL80211_PLINK_CNF_RCVD:
97                         peering = true;
98                         break;
99                 case NL80211_PLINK_ESTAB:
100                         if (sta->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
101                                 light_sleep_cnt++;
102                         else if (sta->mesh->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
103                                 deep_sleep_cnt++;
104                         break;
105                 default:
106                         break;
107                 }
108         }
109         rcu_read_unlock();
110
111         /*
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.
117          */
118         if (peering) {
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;
124         } else {
125                 mps_dbg(sdata, "setting non-peer PM to user value\n");
126                 nonpeer_pm = ifmsh->mshcfg.power_mode;
127         }
128
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;
134
135         ifmsh->nonpeer_pm = nonpeer_pm;
136         ifmsh->ps_peers_light_sleep = light_sleep_cnt;
137         ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
138
139         return changed;
140 }
141
142 /**
143  * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
144  *
145  * @sta: mesh STA
146  * @pm: the power mode to set
147  * Return BSS_CHANGED_BEACON if a beacon update is in order.
148  */
149 u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
150                                    enum nl80211_mesh_power_mode pm)
151 {
152         struct ieee80211_sub_if_data *sdata = sta->sdata;
153
154         if (sta->mesh->local_pm == pm)
155                 return 0;
156
157         mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
158                 pm, sta->sta.addr);
159
160         sta->mesh->local_pm = pm;
161
162         /*
163          * announce peer-specific power mode transition
164          * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
165          */
166         if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
167                 mps_qos_null_tx(sta);
168
169         return ieee80211_mps_local_status_update(sdata);
170 }
171
172 /**
173  * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
174  *
175  * @sdata: local mesh subif
176  * @sta: mesh STA
177  * @hdr: 802.11 frame header
178  *
179  * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
180  *
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
183  */
184 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
185                                    struct sta_info *sta,
186                                    struct ieee80211_hdr *hdr)
187 {
188         enum nl80211_mesh_power_mode pm;
189         u8 *qc;
190
191         if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
192                     ieee80211_is_data_qos(hdr->frame_control) &&
193                     !sta))
194                 return;
195
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;
200         else
201                 pm = sdata->u.mesh.nonpeer_pm;
202
203         if (pm == NL80211_MESH_POWER_ACTIVE)
204                 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
205         else
206                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
207
208         if (!ieee80211_is_data_qos(hdr->frame_control))
209                 return;
210
211         qc = ieee80211_get_qos_ctl(hdr);
212
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);
218         else
219                 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
220 }
221
222 /**
223  * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
224  *
225  * @sta: mesh STA
226  *
227  * called after change of peering status or non-peer/peer-specific power mode
228  */
229 void ieee80211_mps_sta_status_update(struct sta_info *sta)
230 {
231         enum nl80211_mesh_power_mode pm;
232         bool do_buffer;
233
234         /* For non-assoc STA, prevent buffering or frame transmission */
235         if (sta->sta_state < IEEE80211_STA_ASSOC)
236                 return;
237
238         /*
239          * use peer-specific power mode if peering is established and the
240          * peer's power mode is known
241          */
242         if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
243             sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN)
244                 pm = sta->mesh->peer_pm;
245         else
246                 pm = sta->mesh->nonpeer_pm;
247
248         do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
249
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);
256         }
257
258         /* Don't let the same PS state be set twice */
259         if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
260                 return;
261
262         if (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",
266                         sta->sta.addr);
267         } else {
268                 ieee80211_sta_ps_deliver_wakeup(sta);
269         }
270 }
271
272 static void mps_set_sta_peer_pm(struct sta_info *sta,
273                                 struct ieee80211_hdr *hdr)
274 {
275         enum nl80211_mesh_power_mode pm;
276         u8 *qc = ieee80211_get_qos_ctl(hdr);
277
278         /*
279          * Test Power Management field of frame control (PW) and
280          * mesh power save level subfield of QoS control field (PSL)
281          *
282          * | PM | PSL| Mesh PM |
283          * +----+----+---------+
284          * | 0  |Rsrv|  Active |
285          * | 1  | 0  |  Light  |
286          * | 1  | 1  |  Deep   |
287          */
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;
291                 else
292                         pm = NL80211_MESH_POWER_LIGHT_SLEEP;
293         } else {
294                 pm = NL80211_MESH_POWER_ACTIVE;
295         }
296
297         if (sta->mesh->peer_pm == pm)
298                 return;
299
300         mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
301                 sta->sta.addr, pm);
302
303         sta->mesh->peer_pm = pm;
304
305         ieee80211_mps_sta_status_update(sta);
306 }
307
308 static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
309                                    struct ieee80211_hdr *hdr)
310 {
311         enum nl80211_mesh_power_mode pm;
312
313         if (ieee80211_has_pm(hdr->frame_control))
314                 pm = NL80211_MESH_POWER_DEEP_SLEEP;
315         else
316                 pm = NL80211_MESH_POWER_ACTIVE;
317
318         if (sta->mesh->nonpeer_pm == pm)
319                 return;
320
321         mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
322                 sta->sta.addr, pm);
323
324         sta->mesh->nonpeer_pm = pm;
325
326         ieee80211_mps_sta_status_update(sta);
327 }
328
329 /**
330  * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
331  *
332  * @sta: STA info that transmitted the frame
333  * @hdr: IEEE 802.11 (QoS) Header
334  */
335 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
336                                     struct ieee80211_hdr *hdr)
337 {
338         if (is_unicast_ether_addr(hdr->addr1) &&
339             ieee80211_is_data_qos(hdr->frame_control)) {
340                 /*
341                  * individually addressed QoS Data/Null frames contain
342                  * peer link-specific PS mode towards the local STA
343                  */
344                 mps_set_sta_peer_pm(sta, hdr);
345
346                 /* check for mesh Peer Service Period trigger frames */
347                 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
348                                                sta, false, false);
349         } else {
350                 /*
351                  * can only determine non-peer PS mode
352                  * (see IEEE802.11-2012 8.2.4.1.7)
353                  */
354                 mps_set_sta_nonpeer_pm(sta, hdr);
355         }
356 }
357
358
359 /* mesh PS frame release */
360
361 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
362 {
363         struct ieee80211_sub_if_data *sdata = sta->sdata;
364         struct sk_buff *skb;
365         struct ieee80211_hdr *nullfunc;
366         struct ieee80211_tx_info *info;
367         u8 *qc;
368
369         skb = mps_qos_null_get(sta);
370         if (!skb)
371                 return;
372
373         nullfunc = (struct ieee80211_hdr *) skb->data;
374         if (!eosp)
375                 nullfunc->frame_control |=
376                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
377         /*
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
384          */
385         qc = ieee80211_get_qos_ctl(nullfunc);
386         if (rspi)
387                 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
388         if (eosp)
389                 qc[0] |= IEEE80211_QOS_CTL_EOSP;
390
391         info = IEEE80211_SKB_CB(skb);
392
393         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
394                        IEEE80211_TX_CTL_REQ_TX_STATUS;
395
396         mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
397                 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
398
399         ieee80211_tx_skb(sdata, skb);
400 }
401
402 /**
403  * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
404  *
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.
408  */
409 static void mpsp_qos_null_append(struct sta_info *sta,
410                                  struct sk_buff_head *frames)
411 {
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;
416
417         if (ieee80211_is_data_qos(hdr->frame_control))
418                 return;
419
420         new_skb = mps_qos_null_get(sta);
421         if (!new_skb)
422                 return;
423
424         mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
425                 sta->sta.addr);
426         /*
427          * This frame has to be transmitted last. Assign lowest priority to
428          * make sure it cannot pass other frames when releasing multiple ACs.
429          */
430         new_skb->priority = 1;
431         skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
432         ieee80211_set_qos_hdr(sdata, new_skb);
433
434         info = IEEE80211_SKB_CB(new_skb);
435         info->control.vif = &sdata->vif;
436         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
437
438         __skb_queue_tail(frames, new_skb);
439 }
440
441 /**
442  * mps_frame_deliver - transmit frames during mesh powersave
443  *
444  * @sta: STA info to transmit to
445  * @n_frames: number of frames to transmit. -1 for all
446  */
447 static void mps_frame_deliver(struct sta_info *sta, int n_frames)
448 {
449         struct ieee80211_local *local = sta->sdata->local;
450         int ac;
451         struct sk_buff_head frames;
452         struct sk_buff *skb;
453         bool more_data = false;
454
455         skb_queue_head_init(&frames);
456
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]);
461                         if (!skb) {
462                                 skb = skb_dequeue(
463                                         &sta->ps_tx_buf[ac]);
464                                 if (skb)
465                                         local->total_ps_buffered--;
466                         }
467                         if (!skb)
468                                 break;
469                         n_frames--;
470                         __skb_queue_tail(&frames, skb);
471                 }
472
473                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
474                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
475                         more_data = true;
476         }
477
478         /* nothing to send? -> EOSP */
479         if (skb_queue_empty(&frames)) {
480                 mpsp_trigger_send(sta, false, true);
481                 return;
482         }
483
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);
487
488         mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
489                 skb_queue_len(&frames), sta->sta.addr);
490
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;
495
496                 /*
497                  * Tell TX path to send this frame even though the
498                  * STA may still remain is PS mode after this frame
499                  * exchange.
500                  */
501                 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
502
503                 if (more_data || !skb_queue_is_last(&frames, skb))
504                         hdr->frame_control |=
505                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
506                 else
507                         hdr->frame_control &=
508                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
509
510                 if (skb_queue_is_last(&frames, skb) &&
511                     ieee80211_is_data_qos(hdr->frame_control)) {
512                         u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
513
514                         /* MPSP trigger frame ends service period */
515                         *qoshdr |= IEEE80211_QOS_CTL_EOSP;
516                         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
517                 }
518         }
519
520         ieee80211_add_pending_skbs(local, &frames);
521         sta_info_recalc_tim(sta);
522 }
523
524 /**
525  * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
526  *
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
531  *
532  * NOTE: active mode STA may only serve as MPSP owner
533  */
534 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
535                                     bool tx, bool acked)
536 {
537         u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
538         u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
539
540         if (tx) {
541                 if (rspi && acked)
542                         set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
543
544                 if (eosp)
545                         clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
546                 else if (acked &&
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);
550         } else {
551                 if (eosp)
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);
555
556                 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
557                         mps_frame_deliver(sta, -1);
558         }
559 }
560
561 /**
562  * ieee80211_mps_frame_release - release frames buffered due to mesh power save
563  *
564  * @sta: mesh STA
565  * @elems: IEs of beacon or probe response
566  *
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.
571  */
572 void ieee80211_mps_frame_release(struct sta_info *sta,
573                                  struct ieee802_11_elems *elems)
574 {
575         int ac, buffer_local = 0;
576         bool has_buffered = false;
577
578         if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
579                 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
580                                                    sta->mesh->aid);
581
582         if (has_buffered)
583                 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
584                         sta->sta.addr);
585
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)))
589                 return;
590
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]);
595
596         if (!has_buffered && !buffer_local)
597                 return;
598
599         if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
600                 mpsp_trigger_send(sta, has_buffered, !buffer_local);
601         else
602                 mps_frame_deliver(sta, 1);
603 }