1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2012-2013, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de> 4 * Copyright 2012-2013, cozybit Inc. 5 */ 6 7 #include "mesh.h" 8 #include "wme.h" 9 10 11 /* mesh PS management */ 12 13 /** 14 * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave 15 */ 16 static struct sk_buff *mps_qos_null_get(struct sta_info *sta) 17 { 18 struct ieee80211_sub_if_data *sdata = sta->sdata; 19 struct ieee80211_local *local = sdata->local; 20 struct ieee80211_hdr *nullfunc; /* use 4addr header */ 21 struct sk_buff *skb; 22 int size = sizeof(*nullfunc); 23 __le16 fc; 24 25 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2); 26 if (!skb) 27 return NULL; 28 skb_reserve(skb, local->hw.extra_tx_headroom); 29 30 nullfunc = skb_put(skb, size); 31 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_NULLFUNC); 32 ieee80211_fill_mesh_addresses(nullfunc, &fc, sta->sta.addr, 33 sdata->vif.addr); 34 nullfunc->frame_control = fc; 35 nullfunc->duration_id = 0; 36 nullfunc->seq_ctrl = 0; 37 /* no address resolution for this frame -> set addr 1 immediately */ 38 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 39 skb_put_zero(skb, 2); /* append QoS control field */ 40 ieee80211_mps_set_frame_flags(sdata, sta, nullfunc); 41 42 return skb; 43 } 44 45 /** 46 * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode 47 */ 48 static void mps_qos_null_tx(struct sta_info *sta) 49 { 50 struct sk_buff *skb; 51 52 skb = mps_qos_null_get(sta); 53 if (!skb) 54 return; 55 56 mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n", 57 sta->sta.addr); 58 59 /* don't unintentionally start a MPSP */ 60 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 61 u8 *qc = ieee80211_get_qos_ctl((void *) skb->data); 62 63 qc[0] |= IEEE80211_QOS_CTL_EOSP; 64 } 65 66 ieee80211_tx_skb(sta->sdata, skb); 67 } 68 69 /** 70 * ieee80211_mps_local_status_update - track status of local link-specific PMs 71 * 72 * @sdata: local mesh subif 73 * 74 * sets the non-peer power mode and triggers the driver PS (re-)configuration 75 * Return BSS_CHANGED_BEACON if a beacon update is necessary. 76 */ 77 u32 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata) 78 { 79 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 80 struct sta_info *sta; 81 bool peering = false; 82 int light_sleep_cnt = 0; 83 int deep_sleep_cnt = 0; 84 u32 changed = 0; 85 enum nl80211_mesh_power_mode nonpeer_pm; 86 87 rcu_read_lock(); 88 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) { 89 if (sdata != sta->sdata) 90 continue; 91 92 switch (sta->mesh->plink_state) { 93 case NL80211_PLINK_OPN_SNT: 94 case NL80211_PLINK_OPN_RCVD: 95 case NL80211_PLINK_CNF_RCVD: 96 peering = true; 97 break; 98 case NL80211_PLINK_ESTAB: 99 if (sta->mesh->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP) 100 light_sleep_cnt++; 101 else if (sta->mesh->local_pm == NL80211_MESH_POWER_DEEP_SLEEP) 102 deep_sleep_cnt++; 103 break; 104 default: 105 break; 106 } 107 } 108 rcu_read_unlock(); 109 110 /* 111 * Set non-peer mode to active during peering/scanning/authentication 112 * (see IEEE802.11-2012 13.14.8.3). The non-peer mesh power mode is 113 * deep sleep if the local STA is in light or deep sleep towards at 114 * least one mesh peer (see 13.14.3.1). Otherwise, set it to the 115 * user-configured default value. 116 */ 117 if (peering) { 118 mps_dbg(sdata, "setting non-peer PM to active for peering\n"); 119 nonpeer_pm = NL80211_MESH_POWER_ACTIVE; 120 } else if (light_sleep_cnt || deep_sleep_cnt) { 121 mps_dbg(sdata, "setting non-peer PM to deep sleep\n"); 122 nonpeer_pm = NL80211_MESH_POWER_DEEP_SLEEP; 123 } else { 124 mps_dbg(sdata, "setting non-peer PM to user value\n"); 125 nonpeer_pm = ifmsh->mshcfg.power_mode; 126 } 127 128 /* need update if sleep counts move between 0 and non-zero */ 129 if (ifmsh->nonpeer_pm != nonpeer_pm || 130 !ifmsh->ps_peers_light_sleep != !light_sleep_cnt || 131 !ifmsh->ps_peers_deep_sleep != !deep_sleep_cnt) 132 changed = BSS_CHANGED_BEACON; 133 134 ifmsh->nonpeer_pm = nonpeer_pm; 135 ifmsh->ps_peers_light_sleep = light_sleep_cnt; 136 ifmsh->ps_peers_deep_sleep = deep_sleep_cnt; 137 138 return changed; 139 } 140 141 /** 142 * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA 143 * 144 * @sta: mesh STA 145 * @pm: the power mode to set 146 * Return BSS_CHANGED_BEACON if a beacon update is in order. 147 */ 148 u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta, 149 enum nl80211_mesh_power_mode pm) 150 { 151 struct ieee80211_sub_if_data *sdata = sta->sdata; 152 153 if (sta->mesh->local_pm == pm) 154 return 0; 155 156 mps_dbg(sdata, "local STA operates in mode %d with %pM\n", 157 pm, sta->sta.addr); 158 159 sta->mesh->local_pm = pm; 160 161 /* 162 * announce peer-specific power mode transition 163 * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3) 164 */ 165 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB) 166 mps_qos_null_tx(sta); 167 168 return ieee80211_mps_local_status_update(sdata); 169 } 170 171 /** 172 * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control) 173 * 174 * @sdata: local mesh subif 175 * @sta: mesh STA 176 * @hdr: 802.11 frame header 177 * 178 * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11 179 * 180 * NOTE: sta must be given when an individually-addressed QoS frame header 181 * is handled, for group-addressed and management frames it is not used 182 */ 183 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata, 184 struct sta_info *sta, 185 struct ieee80211_hdr *hdr) 186 { 187 enum nl80211_mesh_power_mode pm; 188 u8 *qc; 189 190 if (WARN_ON(is_unicast_ether_addr(hdr->addr1) && 191 ieee80211_is_data_qos(hdr->frame_control) && 192 !sta)) 193 return; 194 195 if (is_unicast_ether_addr(hdr->addr1) && 196 ieee80211_is_data_qos(hdr->frame_control) && 197 sta->mesh->plink_state == NL80211_PLINK_ESTAB) 198 pm = sta->mesh->local_pm; 199 else 200 pm = sdata->u.mesh.nonpeer_pm; 201 202 if (pm == NL80211_MESH_POWER_ACTIVE) 203 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM); 204 else 205 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); 206 207 if (!ieee80211_is_data_qos(hdr->frame_control)) 208 return; 209 210 qc = ieee80211_get_qos_ctl(hdr); 211 212 if ((is_unicast_ether_addr(hdr->addr1) && 213 pm == NL80211_MESH_POWER_DEEP_SLEEP) || 214 (is_multicast_ether_addr(hdr->addr1) && 215 sdata->u.mesh.ps_peers_deep_sleep > 0)) 216 qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8); 217 else 218 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8); 219 } 220 221 /** 222 * ieee80211_mps_sta_status_update - update buffering status of neighbor STA 223 * 224 * @sta: mesh STA 225 * 226 * called after change of peering status or non-peer/peer-specific power mode 227 */ 228 void ieee80211_mps_sta_status_update(struct sta_info *sta) 229 { 230 enum nl80211_mesh_power_mode pm; 231 bool do_buffer; 232 233 /* For non-assoc STA, prevent buffering or frame transmission */ 234 if (sta->sta_state < IEEE80211_STA_ASSOC) 235 return; 236 237 /* 238 * use peer-specific power mode if peering is established and the 239 * peer's power mode is known 240 */ 241 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB && 242 sta->mesh->peer_pm != NL80211_MESH_POWER_UNKNOWN) 243 pm = sta->mesh->peer_pm; 244 else 245 pm = sta->mesh->nonpeer_pm; 246 247 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE); 248 249 /* clear the MPSP flags for non-peers or active STA */ 250 if (sta->mesh->plink_state != NL80211_PLINK_ESTAB) { 251 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER); 252 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT); 253 } else if (!do_buffer) { 254 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER); 255 } 256 257 /* Don't let the same PS state be set twice */ 258 if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer) 259 return; 260 261 if (do_buffer) { 262 set_sta_flag(sta, WLAN_STA_PS_STA); 263 atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps); 264 mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n", 265 sta->sta.addr); 266 } else { 267 ieee80211_sta_ps_deliver_wakeup(sta); 268 } 269 } 270 271 static void mps_set_sta_peer_pm(struct sta_info *sta, 272 struct ieee80211_hdr *hdr) 273 { 274 enum nl80211_mesh_power_mode pm; 275 u8 *qc = ieee80211_get_qos_ctl(hdr); 276 277 /* 278 * Test Power Management field of frame control (PW) and 279 * mesh power save level subfield of QoS control field (PSL) 280 * 281 * | PM | PSL| Mesh PM | 282 * +----+----+---------+ 283 * | 0 |Rsrv| Active | 284 * | 1 | 0 | Light | 285 * | 1 | 1 | Deep | 286 */ 287 if (ieee80211_has_pm(hdr->frame_control)) { 288 if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8)) 289 pm = NL80211_MESH_POWER_DEEP_SLEEP; 290 else 291 pm = NL80211_MESH_POWER_LIGHT_SLEEP; 292 } else { 293 pm = NL80211_MESH_POWER_ACTIVE; 294 } 295 296 if (sta->mesh->peer_pm == pm) 297 return; 298 299 mps_dbg(sta->sdata, "STA %pM enters mode %d\n", 300 sta->sta.addr, pm); 301 302 sta->mesh->peer_pm = pm; 303 304 ieee80211_mps_sta_status_update(sta); 305 } 306 307 static void mps_set_sta_nonpeer_pm(struct sta_info *sta, 308 struct ieee80211_hdr *hdr) 309 { 310 enum nl80211_mesh_power_mode pm; 311 312 if (ieee80211_has_pm(hdr->frame_control)) 313 pm = NL80211_MESH_POWER_DEEP_SLEEP; 314 else 315 pm = NL80211_MESH_POWER_ACTIVE; 316 317 if (sta->mesh->nonpeer_pm == pm) 318 return; 319 320 mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n", 321 sta->sta.addr, pm); 322 323 sta->mesh->nonpeer_pm = pm; 324 325 ieee80211_mps_sta_status_update(sta); 326 } 327 328 /** 329 * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave 330 * 331 * @sta: STA info that transmitted the frame 332 * @hdr: IEEE 802.11 (QoS) Header 333 */ 334 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta, 335 struct ieee80211_hdr *hdr) 336 { 337 if (is_unicast_ether_addr(hdr->addr1) && 338 ieee80211_is_data_qos(hdr->frame_control)) { 339 /* 340 * individually addressed QoS Data/Null frames contain 341 * peer link-specific PS mode towards the local STA 342 */ 343 mps_set_sta_peer_pm(sta, hdr); 344 345 /* check for mesh Peer Service Period trigger frames */ 346 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr), 347 sta, false, false); 348 } else { 349 /* 350 * can only determine non-peer PS mode 351 * (see IEEE802.11-2012 8.2.4.1.7) 352 */ 353 mps_set_sta_nonpeer_pm(sta, hdr); 354 } 355 } 356 357 358 /* mesh PS frame release */ 359 360 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp) 361 { 362 struct ieee80211_sub_if_data *sdata = sta->sdata; 363 struct sk_buff *skb; 364 struct ieee80211_hdr *nullfunc; 365 struct ieee80211_tx_info *info; 366 u8 *qc; 367 368 skb = mps_qos_null_get(sta); 369 if (!skb) 370 return; 371 372 nullfunc = (struct ieee80211_hdr *) skb->data; 373 if (!eosp) 374 nullfunc->frame_control |= 375 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 376 /* 377 * | RSPI | EOSP | MPSP triggering | 378 * +------+------+--------------------+ 379 * | 0 | 0 | local STA is owner | 380 * | 0 | 1 | no MPSP (MPSP end) | 381 * | 1 | 0 | both STA are owner | 382 * | 1 | 1 | peer STA is owner | see IEEE802.11-2012 13.14.9.2 383 */ 384 qc = ieee80211_get_qos_ctl(nullfunc); 385 if (rspi) 386 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8); 387 if (eosp) 388 qc[0] |= IEEE80211_QOS_CTL_EOSP; 389 390 info = IEEE80211_SKB_CB(skb); 391 392 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 393 IEEE80211_TX_CTL_REQ_TX_STATUS; 394 395 mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n", 396 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr); 397 398 ieee80211_tx_skb(sdata, skb); 399 } 400 401 /** 402 * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed 403 * 404 * To properly end a mesh MPSP the last transmitted frame has to set the EOSP 405 * flag in the QoS Control field. In case the current tailing frame is not a 406 * QoS Data frame, append a QoS Null to carry the flag. 407 */ 408 static void mpsp_qos_null_append(struct sta_info *sta, 409 struct sk_buff_head *frames) 410 { 411 struct ieee80211_sub_if_data *sdata = sta->sdata; 412 struct sk_buff *new_skb, *skb = skb_peek_tail(frames); 413 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 414 struct ieee80211_tx_info *info; 415 416 if (ieee80211_is_data_qos(hdr->frame_control)) 417 return; 418 419 new_skb = mps_qos_null_get(sta); 420 if (!new_skb) 421 return; 422 423 mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n", 424 sta->sta.addr); 425 /* 426 * This frame has to be transmitted last. Assign lowest priority to 427 * make sure it cannot pass other frames when releasing multiple ACs. 428 */ 429 new_skb->priority = 1; 430 skb_set_queue_mapping(new_skb, IEEE80211_AC_BK); 431 ieee80211_set_qos_hdr(sdata, new_skb); 432 433 info = IEEE80211_SKB_CB(new_skb); 434 info->control.vif = &sdata->vif; 435 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING; 436 437 __skb_queue_tail(frames, new_skb); 438 } 439 440 /** 441 * mps_frame_deliver - transmit frames during mesh powersave 442 * 443 * @sta: STA info to transmit to 444 * @n_frames: number of frames to transmit. -1 for all 445 */ 446 static void mps_frame_deliver(struct sta_info *sta, int n_frames) 447 { 448 struct ieee80211_local *local = sta->sdata->local; 449 int ac; 450 struct sk_buff_head frames; 451 struct sk_buff *skb; 452 bool more_data = false; 453 454 skb_queue_head_init(&frames); 455 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]); 460 if (!skb) { 461 skb = skb_dequeue( 462 &sta->ps_tx_buf[ac]); 463 if (skb) 464 local->total_ps_buffered--; 465 } 466 if (!skb) 467 break; 468 n_frames--; 469 __skb_queue_tail(&frames, skb); 470 } 471 472 if (!skb_queue_empty(&sta->tx_filtered[ac]) || 473 !skb_queue_empty(&sta->ps_tx_buf[ac])) 474 more_data = true; 475 } 476 477 /* nothing to send? -> EOSP */ 478 if (skb_queue_empty(&frames)) { 479 mpsp_trigger_send(sta, false, true); 480 return; 481 } 482 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); 486 487 mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n", 488 skb_queue_len(&frames), sta->sta.addr); 489 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; 494 495 /* 496 * Tell TX path to send this frame even though the 497 * STA may still remain is PS mode after this frame 498 * exchange. 499 */ 500 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 501 502 if (more_data || !skb_queue_is_last(&frames, skb)) 503 hdr->frame_control |= 504 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 505 else 506 hdr->frame_control &= 507 cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 508 509 if (skb_queue_is_last(&frames, skb) && 510 ieee80211_is_data_qos(hdr->frame_control)) { 511 u8 *qoshdr = ieee80211_get_qos_ctl(hdr); 512 513 /* MPSP trigger frame ends service period */ 514 *qoshdr |= IEEE80211_QOS_CTL_EOSP; 515 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 516 } 517 } 518 519 ieee80211_add_pending_skbs(local, &frames); 520 sta_info_recalc_tim(sta); 521 } 522 523 /** 524 * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods 525 * 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 530 * 531 * NOTE: active mode STA may only serve as MPSP owner 532 */ 533 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta, 534 bool tx, bool acked) 535 { 536 u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8); 537 u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP; 538 539 if (tx) { 540 if (rspi && acked) 541 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT); 542 543 if (eosp) 544 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER); 545 else if (acked && 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); 549 } else { 550 if (eosp) 551 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT); 552 else if (sta->mesh->local_pm != NL80211_MESH_POWER_ACTIVE) 553 set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT); 554 555 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER)) 556 mps_frame_deliver(sta, -1); 557 } 558 } 559 560 /** 561 * ieee80211_mps_frame_release - release frames buffered due to mesh power save 562 * 563 * @sta: mesh STA 564 * @elems: IEs of beacon or probe response 565 * 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. 570 */ 571 void ieee80211_mps_frame_release(struct sta_info *sta, 572 struct ieee802_11_elems *elems) 573 { 574 int ac, buffer_local = 0; 575 bool has_buffered = false; 576 577 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB) 578 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len, 579 sta->mesh->aid); 580 581 if (has_buffered) 582 mps_dbg(sta->sdata, "%pM indicates buffered frames\n", 583 sta->sta.addr); 584 585 /* only transmit to PS STA with announced, non-zero awake window */ 586 if (test_sta_flag(sta, WLAN_STA_PS_STA) && 587 (!elems->awake_window || !le16_to_cpu(*elems->awake_window))) 588 return; 589 590 if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER)) 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]); 594 595 if (!has_buffered && !buffer_local) 596 return; 597 598 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB) 599 mpsp_trigger_send(sta, has_buffered, !buffer_local); 600 else 601 mps_frame_deliver(sta, 1); 602 } 603