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