2 * Copyright 2012-2013, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de>
3 * Copyright 2012-2013, cozybit Inc.
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.
14 /* mesh PS management */
17 * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave
19 static struct sk_buff *mps_qos_null_get(struct sta_info *sta)
21 struct ieee80211_sub_if_data *sdata = sta->sdata;
22 struct ieee80211_local *local = sdata->local;
23 struct ieee80211_hdr *nullfunc; /* use 4addr header */
25 int size = sizeof(*nullfunc);
28 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2);
31 skb_reserve(skb, local->hw.extra_tx_headroom);
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,
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);
48 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
50 static void mps_qos_null_tx(struct sta_info *sta)
54 skb = mps_qos_null_get(sta);
58 mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
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);
65 qc[0] |= IEEE80211_QOS_CTL_EOSP;
68 ieee80211_tx_skb(sta->sdata, skb);
72 * ieee80211_mps_local_status_update - track status of local link-specific PMs
74 * @sdata: local mesh subif
76 * sets the non-peer power mode and triggers the driver PS (re-)configuration
78 void ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
80 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
83 int light_sleep_cnt = 0;
84 int deep_sleep_cnt = 0;
87 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
88 if (sdata != sta->sdata)
91 switch (sta->plink_state) {
92 case NL80211_PLINK_OPN_SNT:
93 case NL80211_PLINK_OPN_RCVD:
94 case NL80211_PLINK_CNF_RCVD:
97 case NL80211_PLINK_ESTAB:
98 if (sta->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
100 else if (sta->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
110 * Set non-peer mode to active during peering/scanning/authentication
111 * (see IEEE802.11-2012 13.14.8.3). The non-peer mesh power mode is
112 * deep sleep if the local STA is in light or deep sleep towards at
113 * least one mesh peer (see 13.14.3.1). Otherwise, set it to the
114 * user-configured default value.
117 mps_dbg(sdata, "setting non-peer PM to active for peering\n");
118 ifmsh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
119 } else if (light_sleep_cnt || deep_sleep_cnt) {
120 mps_dbg(sdata, "setting non-peer PM to deep sleep\n");
121 ifmsh->nonpeer_pm = NL80211_MESH_POWER_DEEP_SLEEP;
123 mps_dbg(sdata, "setting non-peer PM to user value\n");
124 ifmsh->nonpeer_pm = ifmsh->mshcfg.power_mode;
127 ifmsh->ps_peers_light_sleep = light_sleep_cnt;
128 ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
132 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
135 * @pm: the power mode to set
137 void ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
138 enum nl80211_mesh_power_mode pm)
140 struct ieee80211_sub_if_data *sdata = sta->sdata;
142 mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
148 * announce peer-specific power mode transition
149 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
151 if (sta->plink_state == NL80211_PLINK_ESTAB)
152 mps_qos_null_tx(sta);
154 ieee80211_mps_local_status_update(sdata);
158 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
160 * @sdata: local mesh subif
162 * @hdr: 802.11 frame header
164 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
166 * NOTE: sta must be given when an individually-addressed QoS frame header
167 * is handled, for group-addressed and management frames it is not used
169 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
170 struct sta_info *sta,
171 struct ieee80211_hdr *hdr)
173 enum nl80211_mesh_power_mode pm;
176 if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
177 ieee80211_is_data_qos(hdr->frame_control) &&
181 if (is_unicast_ether_addr(hdr->addr1) &&
182 ieee80211_is_data_qos(hdr->frame_control) &&
183 sta->plink_state == NL80211_PLINK_ESTAB)
186 pm = sdata->u.mesh.nonpeer_pm;
188 if (pm == NL80211_MESH_POWER_ACTIVE)
189 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
191 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
193 if (!ieee80211_is_data_qos(hdr->frame_control))
196 qc = ieee80211_get_qos_ctl(hdr);
198 if ((is_unicast_ether_addr(hdr->addr1) &&
199 pm == NL80211_MESH_POWER_DEEP_SLEEP) ||
200 (is_multicast_ether_addr(hdr->addr1) &&
201 sdata->u.mesh.ps_peers_deep_sleep > 0))
202 qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
204 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
208 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
212 * called after change of peering status or non-peer/peer-specific power mode
214 void ieee80211_mps_sta_status_update(struct sta_info *sta)
216 enum nl80211_mesh_power_mode pm;
220 * use peer-specific power mode if peering is established and the
221 * peer's power mode is known
223 if (sta->plink_state == NL80211_PLINK_ESTAB &&
224 sta->peer_pm != NL80211_MESH_POWER_UNKNOWN)
227 pm = sta->nonpeer_pm;
229 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
231 /* Don't let the same PS state be set twice */
232 if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
236 set_sta_flag(sta, WLAN_STA_PS_STA);
237 atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps);
238 mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n",
241 ieee80211_sta_ps_deliver_wakeup(sta);
244 /* clear the MPSP flags for non-peers or active STA */
245 if (sta->plink_state != NL80211_PLINK_ESTAB) {
246 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
247 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
248 } else if (!do_buffer) {
249 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
253 static void mps_set_sta_peer_pm(struct sta_info *sta,
254 struct ieee80211_hdr *hdr)
256 enum nl80211_mesh_power_mode pm;
257 u8 *qc = ieee80211_get_qos_ctl(hdr);
260 * Test Power Management field of frame control (PW) and
261 * mesh power save level subfield of QoS control field (PSL)
263 * | PM | PSL| Mesh PM |
264 * +----+----+---------+
265 * | 0 |Rsrv| Active |
269 if (ieee80211_has_pm(hdr->frame_control)) {
270 if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8))
271 pm = NL80211_MESH_POWER_DEEP_SLEEP;
273 pm = NL80211_MESH_POWER_LIGHT_SLEEP;
275 pm = NL80211_MESH_POWER_ACTIVE;
278 if (sta->peer_pm == pm)
281 mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
286 ieee80211_mps_sta_status_update(sta);
289 static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
290 struct ieee80211_hdr *hdr)
292 enum nl80211_mesh_power_mode pm;
294 if (ieee80211_has_pm(hdr->frame_control))
295 pm = NL80211_MESH_POWER_DEEP_SLEEP;
297 pm = NL80211_MESH_POWER_ACTIVE;
299 if (sta->nonpeer_pm == pm)
302 mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
305 sta->nonpeer_pm = pm;
307 ieee80211_mps_sta_status_update(sta);
311 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
313 * @sta: STA info that transmitted the frame
314 * @hdr: IEEE 802.11 (QoS) Header
316 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
317 struct ieee80211_hdr *hdr)
319 if (is_unicast_ether_addr(hdr->addr1) &&
320 ieee80211_is_data_qos(hdr->frame_control)) {
322 * individually addressed QoS Data/Null frames contain
323 * peer link-specific PS mode towards the local STA
325 mps_set_sta_peer_pm(sta, hdr);
327 /* check for mesh Peer Service Period trigger frames */
328 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
332 * can only determine non-peer PS mode
333 * (see IEEE802.11-2012 8.2.4.1.7)
335 mps_set_sta_nonpeer_pm(sta, hdr);
340 /* mesh PS frame release */
342 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
344 struct ieee80211_sub_if_data *sdata = sta->sdata;
346 struct ieee80211_hdr *nullfunc;
347 struct ieee80211_tx_info *info;
350 skb = mps_qos_null_get(sta);
354 nullfunc = (struct ieee80211_hdr *) skb->data;
356 nullfunc->frame_control |=
357 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
359 * | RSPI | EOSP | MPSP triggering |
360 * +------+------+--------------------+
361 * | 0 | 0 | local STA is owner |
362 * | 0 | 1 | no MPSP (MPSP end) |
363 * | 1 | 0 | both STA are owner |
364 * | 1 | 1 | peer STA is owner | see IEEE802.11-2012 13.14.9.2
366 qc = ieee80211_get_qos_ctl(nullfunc);
368 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
370 qc[0] |= IEEE80211_QOS_CTL_EOSP;
372 info = IEEE80211_SKB_CB(skb);
374 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
375 IEEE80211_TX_CTL_REQ_TX_STATUS;
377 mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
378 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
380 ieee80211_tx_skb(sdata, skb);
384 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
386 * To properly end a mesh MPSP the last transmitted frame has to set the EOSP
387 * flag in the QoS Control field. In case the current tailing frame is not a
388 * QoS Data frame, append a QoS Null to carry the flag.
390 static void mpsp_qos_null_append(struct sta_info *sta,
391 struct sk_buff_head *frames)
393 struct ieee80211_sub_if_data *sdata = sta->sdata;
394 struct sk_buff *new_skb, *skb = skb_peek_tail(frames);
395 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
396 struct ieee80211_tx_info *info;
398 if (ieee80211_is_data_qos(hdr->frame_control))
401 new_skb = mps_qos_null_get(sta);
405 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
408 * This frame has to be transmitted last. Assign lowest priority to
409 * make sure it cannot pass other frames when releasing multiple ACs.
411 new_skb->priority = 1;
412 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
413 ieee80211_set_qos_hdr(sdata, new_skb);
415 info = IEEE80211_SKB_CB(new_skb);
416 info->control.vif = &sdata->vif;
417 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
419 __skb_queue_tail(frames, new_skb);
423 * mps_frame_deliver - transmit frames during mesh powersave
425 * @sta: STA info to transmit to
426 * @n_frames: number of frames to transmit. -1 for all
428 static void mps_frame_deliver(struct sta_info *sta, int n_frames)
430 struct ieee80211_sub_if_data *sdata = sta->sdata;
431 struct ieee80211_local *local = sdata->local;
433 struct sk_buff_head frames;
435 bool more_data = false;
437 skb_queue_head_init(&frames);
439 /* collect frame(s) from buffers */
440 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
441 while (n_frames != 0) {
442 skb = skb_dequeue(&sta->tx_filtered[ac]);
445 &sta->ps_tx_buf[ac]);
447 local->total_ps_buffered--;
452 __skb_queue_tail(&frames, skb);
455 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
456 !skb_queue_empty(&sta->ps_tx_buf[ac]))
460 /* nothing to send? -> EOSP */
461 if (skb_queue_empty(&frames)) {
462 mpsp_trigger_send(sta, false, true);
466 /* in a MPSP make sure the last skb is a QoS Data frame */
467 if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
468 mpsp_qos_null_append(sta, &frames);
470 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
471 skb_queue_len(&frames), sta->sta.addr);
473 /* prepare collected frames for transmission */
474 skb_queue_walk(&frames, skb) {
475 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
476 struct ieee80211_hdr *hdr = (void *) skb->data;
479 * Tell TX path to send this frame even though the
480 * STA may still remain is PS mode after this frame
483 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
485 if (more_data || !skb_queue_is_last(&frames, skb))
486 hdr->frame_control |=
487 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
489 hdr->frame_control &=
490 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
492 if (skb_queue_is_last(&frames, skb) &&
493 ieee80211_is_data_qos(hdr->frame_control)) {
494 u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
496 /* MPSP trigger frame ends service period */
497 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
498 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
502 ieee80211_add_pending_skbs(local, &frames);
503 sta_info_recalc_tim(sta);
507 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
509 * @qc: QoS Control field
510 * @sta: peer to start a MPSP with
511 * @tx: frame was transmitted by the local STA
512 * @acked: frame has been transmitted successfully
514 * NOTE: active mode STA may only serve as MPSP owner
516 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
519 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
520 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
524 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
527 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
529 test_sta_flag(sta, WLAN_STA_PS_STA) &&
530 !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
531 mps_frame_deliver(sta, -1);
534 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
535 else if (sta->local_pm != NL80211_MESH_POWER_ACTIVE)
536 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
538 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
539 mps_frame_deliver(sta, -1);
544 * ieee80211_mps_frame_release - release buffered frames in response to beacon
549 * For peers if we have individually-addressed frames buffered or the peer
550 * indicates buffered frames, send a corresponding MPSP trigger frame. Since
551 * we do not evaluate the awake window duration, QoS Nulls are used as MPSP
552 * trigger frames. If the neighbour STA is not a peer, only send single frames.
554 void ieee80211_mps_frame_release(struct sta_info *sta,
555 struct ieee802_11_elems *elems)
557 int ac, buffer_local = 0;
558 bool has_buffered = false;
560 /* TIM map only for LLID <= IEEE80211_MAX_AID */
561 if (sta->plink_state == NL80211_PLINK_ESTAB)
562 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
563 le16_to_cpu(sta->llid) % IEEE80211_MAX_AID);
566 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
569 /* only transmit to PS STA with announced, non-zero awake window */
570 if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
571 (!elems->awake_window || !le16_to_cpu(*elems->awake_window)))
574 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
575 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
576 skb_queue_len(&sta->tx_filtered[ac]);
578 if (!has_buffered && !buffer_local)
581 if (sta->plink_state == NL80211_PLINK_ESTAB)
582 mpsp_trigger_send(sta, has_buffered, !buffer_local);
584 mps_frame_deliver(sta, 1);