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
77 * Return BSS_CHANGED_BEACON if a beacon update is necessary.
79 u32 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
81 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
84 int light_sleep_cnt = 0;
85 int deep_sleep_cnt = 0;
87 enum nl80211_mesh_power_mode nonpeer_pm;
90 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
91 if (sdata != sta->sdata)
94 switch (sta->plink_state) {
95 case NL80211_PLINK_OPN_SNT:
96 case NL80211_PLINK_OPN_RCVD:
97 case NL80211_PLINK_CNF_RCVD:
100 case NL80211_PLINK_ESTAB:
101 if (sta->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
103 else if (sta->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
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.
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;
126 mps_dbg(sdata, "setting non-peer PM to user value\n");
127 nonpeer_pm = ifmsh->mshcfg.power_mode;
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;
136 ifmsh->nonpeer_pm = nonpeer_pm;
137 ifmsh->ps_peers_light_sleep = light_sleep_cnt;
138 ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
144 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
147 * @pm: the power mode to set
148 * Return BSS_CHANGED_BEACON if a beacon update is in order.
150 u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
151 enum nl80211_mesh_power_mode pm)
153 struct ieee80211_sub_if_data *sdata = sta->sdata;
155 mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
161 * announce peer-specific power mode transition
162 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
164 if (sta->plink_state == NL80211_PLINK_ESTAB)
165 mps_qos_null_tx(sta);
167 return ieee80211_mps_local_status_update(sdata);
171 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
173 * @sdata: local mesh subif
175 * @hdr: 802.11 frame header
177 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
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
182 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
183 struct sta_info *sta,
184 struct ieee80211_hdr *hdr)
186 enum nl80211_mesh_power_mode pm;
189 if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
190 ieee80211_is_data_qos(hdr->frame_control) &&
194 if (is_unicast_ether_addr(hdr->addr1) &&
195 ieee80211_is_data_qos(hdr->frame_control) &&
196 sta->plink_state == NL80211_PLINK_ESTAB)
199 pm = sdata->u.mesh.nonpeer_pm;
201 if (pm == NL80211_MESH_POWER_ACTIVE)
202 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
204 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
206 if (!ieee80211_is_data_qos(hdr->frame_control))
209 qc = ieee80211_get_qos_ctl(hdr);
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);
217 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
221 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
225 * called after change of peering status or non-peer/peer-specific power mode
227 void ieee80211_mps_sta_status_update(struct sta_info *sta)
229 enum nl80211_mesh_power_mode pm;
232 /* For non-assoc STA, prevent buffering or frame transmission */
233 if (sta->sta_state < IEEE80211_STA_ASSOC)
237 * use peer-specific power mode if peering is established and the
238 * peer's power mode is known
240 if (sta->plink_state == NL80211_PLINK_ESTAB &&
241 sta->peer_pm != NL80211_MESH_POWER_UNKNOWN)
244 pm = sta->nonpeer_pm;
246 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
248 /* Don't let the same PS state be set twice */
249 if (test_sta_flag(sta, WLAN_STA_PS_STA) == 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",
258 ieee80211_sta_ps_deliver_wakeup(sta);
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);
270 static void mps_set_sta_peer_pm(struct sta_info *sta,
271 struct ieee80211_hdr *hdr)
273 enum nl80211_mesh_power_mode pm;
274 u8 *qc = ieee80211_get_qos_ctl(hdr);
277 * Test Power Management field of frame control (PW) and
278 * mesh power save level subfield of QoS control field (PSL)
280 * | PM | PSL| Mesh PM |
281 * +----+----+---------+
282 * | 0 |Rsrv| Active |
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;
290 pm = NL80211_MESH_POWER_LIGHT_SLEEP;
292 pm = NL80211_MESH_POWER_ACTIVE;
295 if (sta->peer_pm == pm)
298 mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
303 ieee80211_mps_sta_status_update(sta);
306 static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
307 struct ieee80211_hdr *hdr)
309 enum nl80211_mesh_power_mode pm;
311 if (ieee80211_has_pm(hdr->frame_control))
312 pm = NL80211_MESH_POWER_DEEP_SLEEP;
314 pm = NL80211_MESH_POWER_ACTIVE;
316 if (sta->nonpeer_pm == pm)
319 mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
322 sta->nonpeer_pm = pm;
324 ieee80211_mps_sta_status_update(sta);
328 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
330 * @sta: STA info that transmitted the frame
331 * @hdr: IEEE 802.11 (QoS) Header
333 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
334 struct ieee80211_hdr *hdr)
336 if (is_unicast_ether_addr(hdr->addr1) &&
337 ieee80211_is_data_qos(hdr->frame_control)) {
339 * individually addressed QoS Data/Null frames contain
340 * peer link-specific PS mode towards the local STA
342 mps_set_sta_peer_pm(sta, hdr);
344 /* check for mesh Peer Service Period trigger frames */
345 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
349 * can only determine non-peer PS mode
350 * (see IEEE802.11-2012 8.2.4.1.7)
352 mps_set_sta_nonpeer_pm(sta, hdr);
357 /* mesh PS frame release */
359 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
361 struct ieee80211_sub_if_data *sdata = sta->sdata;
363 struct ieee80211_hdr *nullfunc;
364 struct ieee80211_tx_info *info;
367 skb = mps_qos_null_get(sta);
371 nullfunc = (struct ieee80211_hdr *) skb->data;
373 nullfunc->frame_control |=
374 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
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
383 qc = ieee80211_get_qos_ctl(nullfunc);
385 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
387 qc[0] |= IEEE80211_QOS_CTL_EOSP;
389 info = IEEE80211_SKB_CB(skb);
391 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
392 IEEE80211_TX_CTL_REQ_TX_STATUS;
394 mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
395 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
397 ieee80211_tx_skb(sdata, skb);
401 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
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.
407 static void mpsp_qos_null_append(struct sta_info *sta,
408 struct sk_buff_head *frames)
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;
415 if (ieee80211_is_data_qos(hdr->frame_control))
418 new_skb = mps_qos_null_get(sta);
422 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
425 * This frame has to be transmitted last. Assign lowest priority to
426 * make sure it cannot pass other frames when releasing multiple ACs.
428 new_skb->priority = 1;
429 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
430 ieee80211_set_qos_hdr(sdata, new_skb);
432 info = IEEE80211_SKB_CB(new_skb);
433 info->control.vif = &sdata->vif;
434 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
436 __skb_queue_tail(frames, new_skb);
440 * mps_frame_deliver - transmit frames during mesh powersave
442 * @sta: STA info to transmit to
443 * @n_frames: number of frames to transmit. -1 for all
445 static void mps_frame_deliver(struct sta_info *sta, int n_frames)
447 struct ieee80211_sub_if_data *sdata = sta->sdata;
448 struct ieee80211_local *local = sdata->local;
450 struct sk_buff_head frames;
452 bool more_data = false;
454 skb_queue_head_init(&frames);
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]);
462 &sta->ps_tx_buf[ac]);
464 local->total_ps_buffered--;
469 __skb_queue_tail(&frames, skb);
472 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
473 !skb_queue_empty(&sta->ps_tx_buf[ac]))
477 /* nothing to send? -> EOSP */
478 if (skb_queue_empty(&frames)) {
479 mpsp_trigger_send(sta, false, true);
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);
487 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
488 skb_queue_len(&frames), sta->sta.addr);
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;
496 * Tell TX path to send this frame even though the
497 * STA may still remain is PS mode after this frame
500 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
502 if (more_data || !skb_queue_is_last(&frames, skb))
503 hdr->frame_control |=
504 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
506 hdr->frame_control &=
507 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
509 if (skb_queue_is_last(&frames, skb) &&
510 ieee80211_is_data_qos(hdr->frame_control)) {
511 u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
513 /* MPSP trigger frame ends service period */
514 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
515 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
519 ieee80211_add_pending_skbs(local, &frames);
520 sta_info_recalc_tim(sta);
524 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
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
531 * NOTE: active mode STA may only serve as MPSP owner
533 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
536 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
537 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
541 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
544 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
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);
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);
555 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
556 mps_frame_deliver(sta, -1);
561 * ieee80211_mps_frame_release - release buffered frames in response to beacon
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.
571 void ieee80211_mps_frame_release(struct sta_info *sta,
572 struct ieee802_11_elems *elems)
574 int ac, buffer_local = 0;
575 bool has_buffered = false;
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);
583 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
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)))
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]);
595 if (!has_buffered && !buffer_local)
598 if (sta->plink_state == NL80211_PLINK_ESTAB)
599 mpsp_trigger_send(sta, has_buffered, !buffer_local);
601 mps_frame_deliver(sta, 1);