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 /* 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); 43 44 return skb; 45 } 46 47 /** 48 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode 49 */ 50 static 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 /* 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); 64 65 qc[0] |= IEEE80211_QOS_CTL_EOSP; 66 } 67 68 ieee80211_tx_skb(sta->sdata, skb); 69 } 70 71 /** 72 * ieee80211_mps_local_status_update - track status of local link-specific PMs 73 * 74 * @sdata: local mesh subif 75 * 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. 78 */ 79 u32 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->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->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP) 102 light_sleep_cnt++; 103 else if (sta->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 * 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. 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 /* 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; 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 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA 145 * 146 * @sta: mesh STA 147 * @pm: the power mode to set 148 * Return BSS_CHANGED_BEACON if a beacon update is in order. 149 */ 150 u32 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->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->local_pm = pm; 162 163 /* 164 * announce peer-specific power mode transition 165 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3) 166 */ 167 if (sta->plink_state == NL80211_PLINK_ESTAB) 168 mps_qos_null_tx(sta); 169 170 return ieee80211_mps_local_status_update(sdata); 171 } 172 173 /** 174 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control) 175 * 176 * @sdata: local mesh subif 177 * @sta: mesh STA 178 * @hdr: 802.11 frame header 179 * 180 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11 181 * 182 * NOTE: sta must be given when an individually-addressed QoS frame header 183 * is handled, for group-addressed and management frames it is not used 184 */ 185 void 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->plink_state == NL80211_PLINK_ESTAB) 200 pm = sta->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 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA 225 * 226 * @sta: mesh STA 227 * 228 * called after change of peering status or non-peer/peer-specific power mode 229 */ 230 void ieee80211_mps_sta_status_update(struct sta_info *sta) 231 { 232 enum nl80211_mesh_power_mode pm; 233 bool do_buffer; 234 235 /* For non-assoc STA, prevent buffering or frame transmission */ 236 if (sta->sta_state < IEEE80211_STA_ASSOC) 237 return; 238 239 /* 240 * use peer-specific power mode if peering is established and the 241 * peer's power mode is known 242 */ 243 if (sta->plink_state == NL80211_PLINK_ESTAB && 244 sta->peer_pm != NL80211_MESH_POWER_UNKNOWN) 245 pm = sta->peer_pm; 246 else 247 pm = sta->nonpeer_pm; 248 249 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE); 250 251 /* clear the MPSP flags for non-peers or active STA */ 252 if (sta->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 /* Don't let the same PS state be set twice */ 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 273 static 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 * Test Power Management field of frame control (PW) and 281 * mesh power save level subfield of QoS control field (PSL) 282 * 283 * | PM | PSL| Mesh PM | 284 * +----+----+---------+ 285 * | 0 |Rsrv| Active | 286 * | 1 | 0 | Light | 287 * | 1 | 1 | Deep | 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->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->peer_pm = pm; 305 306 ieee80211_mps_sta_status_update(sta); 307 } 308 309 static 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->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->nonpeer_pm = pm; 326 327 ieee80211_mps_sta_status_update(sta); 328 } 329 330 /** 331 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave 332 * 333 * @sta: STA info that transmitted the frame 334 * @hdr: IEEE 802.11 (QoS) Header 335 */ 336 void 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 * individually addressed QoS Data/Null frames contain 343 * peer link-specific PS mode towards the local STA 344 */ 345 mps_set_sta_peer_pm(sta, hdr); 346 347 /* check for mesh Peer Service Period trigger frames */ 348 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr), 349 sta, false, false); 350 } else { 351 /* 352 * can only determine non-peer PS mode 353 * (see IEEE802.11-2012 8.2.4.1.7) 354 */ 355 mps_set_sta_nonpeer_pm(sta, hdr); 356 } 357 } 358 359 360 /* mesh PS frame release */ 361 362 static 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 * | RSPI | EOSP | MPSP triggering | 380 * +------+------+--------------------+ 381 * | 0 | 0 | local STA is owner | 382 * | 0 | 1 | no MPSP (MPSP end) | 383 * | 1 | 0 | both STA are owner | 384 * | 1 | 1 | peer STA is owner | see IEEE802.11-2012 13.14.9.2 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 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed 405 * 406 * To properly end a mesh MPSP the last transmitted frame has to set the EOSP 407 * flag in the QoS Control field. In case the current tailing frame is not a 408 * QoS Data frame, append a QoS Null to carry the flag. 409 */ 410 static void mpsp_qos_null_append(struct sta_info *sta, 411 struct sk_buff_head *frames) 412 { 413 struct ieee80211_sub_if_data *sdata = sta->sdata; 414 struct sk_buff *new_skb, *skb = skb_peek_tail(frames); 415 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 416 struct ieee80211_tx_info *info; 417 418 if (ieee80211_is_data_qos(hdr->frame_control)) 419 return; 420 421 new_skb = mps_qos_null_get(sta); 422 if (!new_skb) 423 return; 424 425 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n", 426 sta->sta.addr); 427 /* 428 * This frame has to be transmitted last. Assign lowest priority to 429 * make sure it cannot pass other frames when releasing multiple ACs. 430 */ 431 new_skb->priority = 1; 432 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK); 433 ieee80211_set_qos_hdr(sdata, new_skb); 434 435 info = IEEE80211_SKB_CB(new_skb); 436 info->control.vif = &sdata->vif; 437 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING; 438 439 __skb_queue_tail(frames, new_skb); 440 } 441 442 /** 443 * mps_frame_deliver - transmit frames during mesh powersave 444 * 445 * @sta: STA info to transmit to 446 * @n_frames: number of frames to transmit. -1 for all 447 */ 448 static void mps_frame_deliver(struct sta_info *sta, int n_frames) 449 { 450 struct ieee80211_local *local = sta->sdata->local; 451 int ac; 452 struct sk_buff_head frames; 453 struct sk_buff *skb; 454 bool more_data = false; 455 456 skb_queue_head_init(&frames); 457 458 /* collect frame(s) from buffers */ 459 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 460 while (n_frames != 0) { 461 skb = skb_dequeue(&sta->tx_filtered[ac]); 462 if (!skb) { 463 skb = skb_dequeue( 464 &sta->ps_tx_buf[ac]); 465 if (skb) 466 local->total_ps_buffered--; 467 } 468 if (!skb) 469 break; 470 n_frames--; 471 __skb_queue_tail(&frames, skb); 472 } 473 474 if (!skb_queue_empty(&sta->tx_filtered[ac]) || 475 !skb_queue_empty(&sta->ps_tx_buf[ac])) 476 more_data = true; 477 } 478 479 /* nothing to send? -> EOSP */ 480 if (skb_queue_empty(&frames)) { 481 mpsp_trigger_send(sta, false, true); 482 return; 483 } 484 485 /* in a MPSP make sure the last skb is a QoS Data frame */ 486 if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER)) 487 mpsp_qos_null_append(sta, &frames); 488 489 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n", 490 skb_queue_len(&frames), sta->sta.addr); 491 492 /* prepare collected frames for transmission */ 493 skb_queue_walk(&frames, skb) { 494 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 495 struct ieee80211_hdr *hdr = (void *) skb->data; 496 497 /* 498 * Tell TX path to send this frame even though the 499 * STA may still remain is PS mode after this frame 500 * exchange. 501 */ 502 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 503 504 if (more_data || !skb_queue_is_last(&frames, skb)) 505 hdr->frame_control |= 506 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 507 else 508 hdr->frame_control &= 509 cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 510 511 if (skb_queue_is_last(&frames, skb) && 512 ieee80211_is_data_qos(hdr->frame_control)) { 513 u8 *qoshdr = ieee80211_get_qos_ctl(hdr); 514 515 /* MPSP trigger frame ends service period */ 516 *qoshdr |= IEEE80211_QOS_CTL_EOSP; 517 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 518 } 519 } 520 521 ieee80211_add_pending_skbs(local, &frames); 522 sta_info_recalc_tim(sta); 523 } 524 525 /** 526 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods 527 * 528 * @qc: QoS Control field 529 * @sta: peer to start a MPSP with 530 * @tx: frame was transmitted by the local STA 531 * @acked: frame has been transmitted successfully 532 * 533 * NOTE: active mode STA may only serve as MPSP owner 534 */ 535 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta, 536 bool tx, bool acked) 537 { 538 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8); 539 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP; 540 541 if (tx) { 542 if (rspi && acked) 543 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT); 544 545 if (eosp) 546 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER); 547 else if (acked && 548 test_sta_flag(sta, WLAN_STA_PS_STA) && 549 !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER)) 550 mps_frame_deliver(sta, -1); 551 } else { 552 if (eosp) 553 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT); 554 else if (sta->local_pm != NL80211_MESH_POWER_ACTIVE) 555 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT); 556 557 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER)) 558 mps_frame_deliver(sta, -1); 559 } 560 } 561 562 /** 563 * ieee80211_mps_frame_release - release frames buffered due to mesh power save 564 * 565 * @sta: mesh STA 566 * @elems: IEs of beacon or probe response 567 * 568 * For peers if we have individually-addressed frames buffered or the peer 569 * indicates buffered frames, send a corresponding MPSP trigger frame. Since 570 * we do not evaluate the awake window duration, QoS Nulls are used as MPSP 571 * trigger frames. If the neighbour STA is not a peer, only send single frames. 572 */ 573 void ieee80211_mps_frame_release(struct sta_info *sta, 574 struct ieee802_11_elems *elems) 575 { 576 int ac, buffer_local = 0; 577 bool has_buffered = false; 578 579 if (sta->plink_state == NL80211_PLINK_ESTAB) 580 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len, 581 sta->llid); 582 583 if (has_buffered) 584 mps_dbg(sta->sdata, "%pM indicates buffered frames\n", 585 sta->sta.addr); 586 587 /* only transmit to PS STA with announced, non-zero awake window */ 588 if (test_sta_flag(sta, WLAN_STA_PS_STA) && 589 (!elems->awake_window || !le16_to_cpu(*elems->awake_window))) 590 return; 591 592 if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER)) 593 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 594 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) + 595 skb_queue_len(&sta->tx_filtered[ac]); 596 597 if (!has_buffered && !buffer_local) 598 return; 599 600 if (sta->plink_state == NL80211_PLINK_ESTAB) 601 mpsp_trigger_send(sta, has_buffered, !buffer_local); 602 else 603 mps_frame_deliver(sta, 1); 604 } 605