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