1
2
3
4
5
6
7
8
9
10#include "mesh.h"
11#include "wme.h"
12
13
14
15
16
17
18
19static 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;
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 = 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
41 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
42 skb_put_zero(skb, 2);
43 ieee80211_mps_set_frame_flags(sdata, sta, nullfunc);
44
45 return skb;
46}
47
48
49
50
51static 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
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
74
75
76
77
78
79
80u32 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->mesh->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->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
103 light_sleep_cnt++;
104 else if (sta->mesh->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
115
116
117
118
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
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
146
147
148
149
150
151u32 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 if (sta->mesh->local_pm == pm)
157 return 0;
158
159 mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
160 pm, sta->sta.addr);
161
162 sta->mesh->local_pm = pm;
163
164
165
166
167
168 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
169 mps_qos_null_tx(sta);
170
171 return ieee80211_mps_local_status_update(sdata);
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
187 struct sta_info *sta,
188 struct ieee80211_hdr *hdr)
189{
190 enum nl80211_mesh_power_mode pm;
191 u8 *qc;
192
193 if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
194 ieee80211_is_data_qos(hdr->frame_control) &&
195 !sta))
196 return;
197
198 if (is_unicast_ether_addr(hdr->addr1) &&
199 ieee80211_is_data_qos(hdr->frame_control) &&
200 sta->mesh->plink_state == NL80211_PLINK_ESTAB)
201 pm = sta->mesh->local_pm;
202 else
203 pm = sdata->u.mesh.nonpeer_pm;
204
205 if (pm == NL80211_MESH_POWER_ACTIVE)
206 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
207 else
208 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
209
210 if (!ieee80211_is_data_qos(hdr->frame_control))
211 return;
212
213 qc = ieee80211_get_qos_ctl(hdr);
214
215 if ((is_unicast_ether_addr(hdr->addr1) &&
216 pm == NL80211_MESH_POWER_DEEP_SLEEP) ||
217 (is_multicast_ether_addr(hdr->addr1) &&
218 sdata->u.mesh.ps_peers_deep_sleep > 0))
219 qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
220 else
221 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
222}
223
224
225
226
227
228
229
230
231void ieee80211_mps_sta_status_update(struct sta_info *sta)
232{
233 enum nl80211_mesh_power_mode pm;
234 bool do_buffer;
235
236
237 if (sta->sta_state < IEEE80211_STA_ASSOC)
238 return;
239
240
241
242
243
244 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
245 sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN)
246 pm = sta->mesh->peer_pm;
247 else
248 pm = sta->mesh->nonpeer_pm;
249
250 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
251
252
253 if (sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
254 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
255 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
256 } else if (!do_buffer) {
257 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
258 }
259
260
261 if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
262 return;
263
264 if (do_buffer) {
265 set_sta_flag(sta, WLAN_STA_PS_STA);
266 atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps);
267 mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n",
268 sta->sta.addr);
269 } else {
270 ieee80211_sta_ps_deliver_wakeup(sta);
271 }
272}
273
274static void mps_set_sta_peer_pm(struct sta_info *sta,
275 struct ieee80211_hdr *hdr)
276{
277 enum nl80211_mesh_power_mode pm;
278 u8 *qc = ieee80211_get_qos_ctl(hdr);
279
280
281
282
283
284
285
286
287
288
289
290 if (ieee80211_has_pm(hdr->frame_control)) {
291 if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8))
292 pm = NL80211_MESH_POWER_DEEP_SLEEP;
293 else
294 pm = NL80211_MESH_POWER_LIGHT_SLEEP;
295 } else {
296 pm = NL80211_MESH_POWER_ACTIVE;
297 }
298
299 if (sta->mesh->peer_pm == pm)
300 return;
301
302 mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
303 sta->sta.addr, pm);
304
305 sta->mesh->peer_pm = pm;
306
307 ieee80211_mps_sta_status_update(sta);
308}
309
310static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
311 struct ieee80211_hdr *hdr)
312{
313 enum nl80211_mesh_power_mode pm;
314
315 if (ieee80211_has_pm(hdr->frame_control))
316 pm = NL80211_MESH_POWER_DEEP_SLEEP;
317 else
318 pm = NL80211_MESH_POWER_ACTIVE;
319
320 if (sta->mesh->nonpeer_pm == pm)
321 return;
322
323 mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
324 sta->sta.addr, pm);
325
326 sta->mesh->nonpeer_pm = pm;
327
328 ieee80211_mps_sta_status_update(sta);
329}
330
331
332
333
334
335
336
337void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
338 struct ieee80211_hdr *hdr)
339{
340 if (is_unicast_ether_addr(hdr->addr1) &&
341 ieee80211_is_data_qos(hdr->frame_control)) {
342
343
344
345
346 mps_set_sta_peer_pm(sta, hdr);
347
348
349 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
350 sta, false, false);
351 } else {
352
353
354
355
356 mps_set_sta_nonpeer_pm(sta, hdr);
357 }
358}
359
360
361
362
363static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
364{
365 struct ieee80211_sub_if_data *sdata = sta->sdata;
366 struct sk_buff *skb;
367 struct ieee80211_hdr *nullfunc;
368 struct ieee80211_tx_info *info;
369 u8 *qc;
370
371 skb = mps_qos_null_get(sta);
372 if (!skb)
373 return;
374
375 nullfunc = (struct ieee80211_hdr *) skb->data;
376 if (!eosp)
377 nullfunc->frame_control |=
378 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
379
380
381
382
383
384
385
386
387 qc = ieee80211_get_qos_ctl(nullfunc);
388 if (rspi)
389 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
390 if (eosp)
391 qc[0] |= IEEE80211_QOS_CTL_EOSP;
392
393 info = IEEE80211_SKB_CB(skb);
394
395 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
396 IEEE80211_TX_CTL_REQ_TX_STATUS;
397
398 mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
399 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
400
401 ieee80211_tx_skb(sdata, skb);
402}
403
404
405
406
407
408
409
410
411static void mpsp_qos_null_append(struct sta_info *sta,
412 struct sk_buff_head *frames)
413{
414 struct ieee80211_sub_if_data *sdata = sta->sdata;
415 struct sk_buff *new_skb, *skb = skb_peek_tail(frames);
416 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
417 struct ieee80211_tx_info *info;
418
419 if (ieee80211_is_data_qos(hdr->frame_control))
420 return;
421
422 new_skb = mps_qos_null_get(sta);
423 if (!new_skb)
424 return;
425
426 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
427 sta->sta.addr);
428
429
430
431
432 new_skb->priority = 1;
433 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
434 ieee80211_set_qos_hdr(sdata, new_skb);
435
436 info = IEEE80211_SKB_CB(new_skb);
437 info->control.vif = &sdata->vif;
438 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
439
440 __skb_queue_tail(frames, new_skb);
441}
442
443
444
445
446
447
448
449static void mps_frame_deliver(struct sta_info *sta, int n_frames)
450{
451 struct ieee80211_local *local = sta->sdata->local;
452 int ac;
453 struct sk_buff_head frames;
454 struct sk_buff *skb;
455 bool more_data = false;
456
457 skb_queue_head_init(&frames);
458
459
460 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
461 while (n_frames != 0) {
462 skb = skb_dequeue(&sta->tx_filtered[ac]);
463 if (!skb) {
464 skb = skb_dequeue(
465 &sta->ps_tx_buf[ac]);
466 if (skb)
467 local->total_ps_buffered--;
468 }
469 if (!skb)
470 break;
471 n_frames--;
472 __skb_queue_tail(&frames, skb);
473 }
474
475 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
476 !skb_queue_empty(&sta->ps_tx_buf[ac]))
477 more_data = true;
478 }
479
480
481 if (skb_queue_empty(&frames)) {
482 mpsp_trigger_send(sta, false, true);
483 return;
484 }
485
486
487 if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
488 mpsp_qos_null_append(sta, &frames);
489
490 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
491 skb_queue_len(&frames), sta->sta.addr);
492
493
494 skb_queue_walk(&frames, skb) {
495 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
496 struct ieee80211_hdr *hdr = (void *) skb->data;
497
498
499
500
501
502
503 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
504
505 if (more_data || !skb_queue_is_last(&frames, skb))
506 hdr->frame_control |=
507 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
508 else
509 hdr->frame_control &=
510 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
511
512 if (skb_queue_is_last(&frames, skb) &&
513 ieee80211_is_data_qos(hdr->frame_control)) {
514 u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
515
516
517 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
518 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
519 }
520 }
521
522 ieee80211_add_pending_skbs(local, &frames);
523 sta_info_recalc_tim(sta);
524}
525
526
527
528
529
530
531
532
533
534
535
536void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
537 bool tx, bool acked)
538{
539 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
540 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
541
542 if (tx) {
543 if (rspi && acked)
544 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
545
546 if (eosp)
547 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
548 else if (acked &&
549 test_sta_flag(sta, WLAN_STA_PS_STA) &&
550 !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
551 mps_frame_deliver(sta, -1);
552 } else {
553 if (eosp)
554 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
555 else if (sta->mesh->local_pm != NL80211_MESH_POWER_ACTIVE)
556 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
557
558 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
559 mps_frame_deliver(sta, -1);
560 }
561}
562
563
564
565
566
567
568
569
570
571
572
573
574void ieee80211_mps_frame_release(struct sta_info *sta,
575 struct ieee802_11_elems *elems)
576{
577 int ac, buffer_local = 0;
578 bool has_buffered = false;
579
580 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
581 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
582 sta->mesh->aid);
583
584 if (has_buffered)
585 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
586 sta->sta.addr);
587
588
589 if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
590 (!elems->awake_window || !le16_to_cpu(*elems->awake_window)))
591 return;
592
593 if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
594 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
595 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
596 skb_queue_len(&sta->tx_filtered[ac]);
597
598 if (!has_buffered && !buffer_local)
599 return;
600
601 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
602 mpsp_trigger_send(sta, has_buffered, !buffer_local);
603 else
604 mps_frame_deliver(sta, 1);
605}
606