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