1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2002-2005, Instant802 Networks, Inc. 4 * Copyright 2005-2006, Devicescape Software, Inc. 5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 6 * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net> 7 * Copyright 2013-2014 Intel Mobile Communications GmbH 8 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH 9 * Copyright (C) 2018-2026 Intel Corporation 10 */ 11 12 #include <linux/jiffies.h> 13 #include <linux/slab.h> 14 #include <linux/kernel.h> 15 #include <linux/skbuff.h> 16 #include <linux/netdevice.h> 17 #include <linux/etherdevice.h> 18 #include <linux/rcupdate.h> 19 #include <linux/export.h> 20 #include <linux/kcov.h> 21 #include <linux/bitops.h> 22 #include <kunit/visibility.h> 23 #include <net/mac80211.h> 24 #include <net/ieee80211_radiotap.h> 25 #include <linux/unaligned.h> 26 27 #include "ieee80211_i.h" 28 #include "driver-ops.h" 29 #include "led.h" 30 #include "mesh.h" 31 #include "wep.h" 32 #include "wpa.h" 33 #include "tkip.h" 34 #include "wme.h" 35 #include "rate.h" 36 37 /* 38 * monitor mode reception 39 * 40 * This function cleans up the SKB, i.e. it removes all the stuff 41 * only useful for monitoring. 42 */ 43 static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb, 44 unsigned int present_fcs_len, 45 unsigned int rtap_space) 46 { 47 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 48 struct ieee80211_hdr *hdr; 49 unsigned int hdrlen; 50 __le16 fc; 51 52 if (present_fcs_len) 53 __pskb_trim(skb, skb->len - present_fcs_len); 54 pskb_pull(skb, rtap_space); 55 56 /* After pulling radiotap header, clear all flags that indicate 57 * info in skb->data. 58 */ 59 status->flag &= ~(RX_FLAG_RADIOTAP_TLV_AT_END | 60 RX_FLAG_RADIOTAP_LSIG | 61 RX_FLAG_RADIOTAP_HE_MU | 62 RX_FLAG_RADIOTAP_HE | 63 RX_FLAG_RADIOTAP_VHT); 64 65 hdr = (void *)skb->data; 66 fc = hdr->frame_control; 67 68 /* 69 * Remove the HT-Control field (if present) on management 70 * frames after we've sent the frame to monitoring. We 71 * (currently) don't need it, and don't properly parse 72 * frames with it present, due to the assumption of a 73 * fixed management header length. 74 */ 75 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc))) 76 return skb; 77 78 hdrlen = ieee80211_hdrlen(fc); 79 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER); 80 81 if (!pskb_may_pull(skb, hdrlen)) { 82 dev_kfree_skb(skb); 83 return NULL; 84 } 85 86 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data, 87 hdrlen - IEEE80211_HT_CTL_LEN); 88 pskb_pull(skb, IEEE80211_HT_CTL_LEN); 89 90 return skb; 91 } 92 93 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len, 94 unsigned int rtap_space) 95 { 96 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 97 struct ieee80211_hdr *hdr; 98 99 hdr = (void *)(skb->data + rtap_space); 100 101 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | 102 RX_FLAG_FAILED_PLCP_CRC | 103 RX_FLAG_ONLY_MONITOR | 104 RX_FLAG_NO_PSDU)) 105 return true; 106 107 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space)) 108 return true; 109 110 if (ieee80211_is_ctl(hdr->frame_control) && 111 !ieee80211_is_pspoll(hdr->frame_control) && 112 !ieee80211_is_back_req(hdr->frame_control)) 113 return true; 114 115 return false; 116 } 117 118 static int 119 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local, 120 struct ieee80211_rx_status *status, 121 struct sk_buff *skb) 122 { 123 int len; 124 125 /* always present fields */ 126 len = sizeof(struct ieee80211_radiotap_header) + 8; 127 128 /* allocate extra bitmaps */ 129 if (status->chains) 130 len += 4 * hweight8(status->chains); 131 132 if (ieee80211_have_rx_timestamp(status)) { 133 len = ALIGN(len, 8); 134 len += 8; 135 } 136 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) 137 len += 1; 138 139 /* antenna field, if we don't have per-chain info */ 140 if (!status->chains) 141 len += 1; 142 143 /* padding for RX_FLAGS if necessary */ 144 len = ALIGN(len, 2); 145 146 if (status->encoding == RX_ENC_HT) /* HT info */ 147 len += 3; 148 149 if (status->flag & RX_FLAG_AMPDU_DETAILS) { 150 len = ALIGN(len, 4); 151 len += 8; 152 } 153 154 if (status->encoding == RX_ENC_VHT) { 155 /* Included even if RX_FLAG_RADIOTAP_VHT is not set */ 156 len = ALIGN(len, 2); 157 len += 12; 158 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_vht) != 12); 159 } 160 161 if (local->hw.radiotap_timestamp.units_pos >= 0) { 162 len = ALIGN(len, 8); 163 len += 12; 164 } 165 166 if (status->encoding == RX_ENC_HE && 167 status->flag & RX_FLAG_RADIOTAP_HE) { 168 len = ALIGN(len, 2); 169 len += 12; 170 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12); 171 } 172 173 if (status->encoding == RX_ENC_HE && 174 status->flag & RX_FLAG_RADIOTAP_HE_MU) { 175 len = ALIGN(len, 2); 176 len += 12; 177 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12); 178 } 179 180 if (status->flag & RX_FLAG_NO_PSDU) 181 len += 1; 182 183 if (status->flag & RX_FLAG_RADIOTAP_LSIG) { 184 len = ALIGN(len, 2); 185 len += 4; 186 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4); 187 } 188 189 if (status->chains) { 190 /* antenna and antenna signal fields */ 191 len += 2 * hweight8(status->chains); 192 } 193 194 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) { 195 int tlv_offset = 0; 196 197 /* 198 * The position to look at depends on the existence (or non- 199 * existence) of other elements, so take that into account... 200 */ 201 if (status->flag & RX_FLAG_RADIOTAP_VHT) 202 tlv_offset += 203 sizeof(struct ieee80211_radiotap_vht); 204 if (status->flag & RX_FLAG_RADIOTAP_HE) 205 tlv_offset += 206 sizeof(struct ieee80211_radiotap_he); 207 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) 208 tlv_offset += 209 sizeof(struct ieee80211_radiotap_he_mu); 210 if (status->flag & RX_FLAG_RADIOTAP_LSIG) 211 tlv_offset += 212 sizeof(struct ieee80211_radiotap_lsig); 213 214 /* ensure 4 byte alignment for TLV */ 215 len = ALIGN(len, 4); 216 217 /* TLVs until the mac header */ 218 len += skb_mac_header(skb) - &skb->data[tlv_offset]; 219 } 220 221 return len; 222 } 223 224 static void __ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata, 225 int link_id, 226 struct sta_info *sta, 227 struct sk_buff *skb) 228 { 229 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 230 231 if (link_id >= 0) { 232 status->link_valid = 1; 233 status->link_id = link_id; 234 } else { 235 status->link_valid = 0; 236 } 237 238 skb_queue_tail(&sdata->skb_queue, skb); 239 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work); 240 if (sta) { 241 struct link_sta_info *link_sta_info; 242 243 if (link_id >= 0) { 244 link_sta_info = rcu_dereference(sta->link[link_id]); 245 if (!link_sta_info) 246 return; 247 } else { 248 link_sta_info = &sta->deflink; 249 } 250 251 link_sta_info->rx_stats.packets++; 252 } 253 } 254 255 static void ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata, 256 int link_id, 257 struct sta_info *sta, 258 struct sk_buff *skb) 259 { 260 skb->protocol = 0; 261 __ieee80211_queue_skb_to_iface(sdata, link_id, sta, skb); 262 } 263 264 static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata, 265 struct sk_buff *skb, 266 int rtap_space) 267 { 268 struct { 269 struct ieee80211_hdr_3addr hdr; 270 u8 category; 271 u8 action_code; 272 } __packed __aligned(2) action; 273 274 if (!sdata) 275 return; 276 277 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE(action_code)); 278 279 if (skb->len < rtap_space + sizeof(action) + 280 VHT_MUMIMO_GROUPS_DATA_LEN) 281 return; 282 283 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr)) 284 return; 285 286 skb_copy_bits(skb, rtap_space, &action, sizeof(action)); 287 288 if (!ieee80211_is_action(action.hdr.frame_control)) 289 return; 290 291 if (action.category != WLAN_CATEGORY_VHT) 292 return; 293 294 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT) 295 return; 296 297 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr)) 298 return; 299 300 skb = skb_copy(skb, GFP_ATOMIC); 301 if (!skb) 302 return; 303 304 ieee80211_queue_skb_to_iface(sdata, -1, NULL, skb); 305 } 306 307 /* 308 * ieee80211_add_rx_radiotap_header - add radiotap header 309 * 310 * add a radiotap header containing all the fields which the hardware provided. 311 */ 312 static void 313 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local, 314 struct sk_buff *skb, 315 struct ieee80211_rate *rate, 316 int rtap_len, bool has_fcs) 317 { 318 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 319 struct ieee80211_radiotap_header *rthdr; 320 unsigned char *pos; 321 __le32 *it_present; 322 u32 it_present_val; 323 u16 rx_flags = 0; 324 u16 channel_flags = 0; 325 u32 tlvs_len = 0; 326 int mpdulen, chain; 327 unsigned long chains = status->chains; 328 struct ieee80211_radiotap_vht vht = {}; 329 struct ieee80211_radiotap_he he = {}; 330 struct ieee80211_radiotap_he_mu he_mu = {}; 331 struct ieee80211_radiotap_lsig lsig = {}; 332 333 if (status->flag & RX_FLAG_RADIOTAP_VHT) { 334 vht = *(struct ieee80211_radiotap_vht *)skb->data; 335 skb_pull(skb, sizeof(vht)); 336 WARN_ON_ONCE(status->encoding != RX_ENC_VHT); 337 } 338 339 if (status->flag & RX_FLAG_RADIOTAP_HE) { 340 he = *(struct ieee80211_radiotap_he *)skb->data; 341 skb_pull(skb, sizeof(he)); 342 WARN_ON_ONCE(status->encoding != RX_ENC_HE); 343 } 344 345 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) { 346 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data; 347 skb_pull(skb, sizeof(he_mu)); 348 } 349 350 if (status->flag & RX_FLAG_RADIOTAP_LSIG) { 351 lsig = *(struct ieee80211_radiotap_lsig *)skb->data; 352 skb_pull(skb, sizeof(lsig)); 353 } 354 355 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) { 356 /* data is pointer at tlv all other info was pulled off */ 357 tlvs_len = skb_mac_header(skb) - skb->data; 358 } 359 360 mpdulen = skb->len; 361 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))) 362 mpdulen += FCS_LEN; 363 364 rthdr = skb_push(skb, rtap_len - tlvs_len); 365 memset(rthdr, 0, rtap_len - tlvs_len); 366 it_present = &rthdr->it_present; 367 368 /* radiotap header, set always present flags */ 369 rthdr->it_len = cpu_to_le16(rtap_len); 370 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) | 371 BIT(IEEE80211_RADIOTAP_CHANNEL) | 372 BIT(IEEE80211_RADIOTAP_RX_FLAGS); 373 374 if (!status->chains) 375 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA); 376 377 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) { 378 it_present_val |= 379 BIT(IEEE80211_RADIOTAP_EXT) | 380 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE); 381 put_unaligned_le32(it_present_val, it_present); 382 it_present++; 383 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) | 384 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL); 385 } 386 387 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) 388 it_present_val |= BIT(IEEE80211_RADIOTAP_TLV); 389 390 put_unaligned_le32(it_present_val, it_present); 391 392 /* This references through an offset into it_optional[] rather 393 * than via it_present otherwise later uses of pos will cause 394 * the compiler to think we have walked past the end of the 395 * struct member. 396 */ 397 pos = (void *)&rthdr->it_optional[it_present + 1 - rthdr->it_optional]; 398 399 /* the order of the following fields is important */ 400 401 /* IEEE80211_RADIOTAP_TSFT */ 402 if (ieee80211_have_rx_timestamp(status)) { 403 /* padding */ 404 while ((pos - (u8 *)rthdr) & 7) 405 *pos++ = 0; 406 put_unaligned_le64( 407 ieee80211_calculate_rx_timestamp(&local->hw, status, 408 mpdulen, 0), 409 pos); 410 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_TSFT)); 411 pos += 8; 412 } 413 414 /* IEEE80211_RADIOTAP_FLAGS */ 415 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) 416 *pos |= IEEE80211_RADIOTAP_F_FCS; 417 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC)) 418 *pos |= IEEE80211_RADIOTAP_F_BADFCS; 419 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) 420 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE; 421 pos++; 422 423 /* IEEE80211_RADIOTAP_RATE */ 424 if (!rate || status->encoding != RX_ENC_LEGACY) { 425 /* 426 * Without rate information don't add it. If we have, 427 * MCS information is a separate field in radiotap, 428 * added below. The byte here is needed as padding 429 * for the channel though, so initialise it to 0. 430 */ 431 *pos = 0; 432 } else { 433 int shift = 0; 434 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_RATE)); 435 if (status->bw == RATE_INFO_BW_10) 436 shift = 1; 437 else if (status->bw == RATE_INFO_BW_5) 438 shift = 2; 439 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift)); 440 } 441 pos++; 442 443 /* IEEE80211_RADIOTAP_CHANNEL */ 444 /* TODO: frequency offset in KHz */ 445 put_unaligned_le16(status->freq, pos); 446 pos += 2; 447 if (status->bw == RATE_INFO_BW_10) 448 channel_flags |= IEEE80211_CHAN_HALF; 449 else if (status->bw == RATE_INFO_BW_5) 450 channel_flags |= IEEE80211_CHAN_QUARTER; 451 452 if (status->band == NL80211_BAND_5GHZ || 453 status->band == NL80211_BAND_6GHZ) 454 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ; 455 else if (status->encoding != RX_ENC_LEGACY) 456 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 457 else if (rate && rate->flags & IEEE80211_RATE_ERP_G) 458 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ; 459 else if (rate) 460 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ; 461 else 462 channel_flags |= IEEE80211_CHAN_2GHZ; 463 put_unaligned_le16(channel_flags, pos); 464 pos += 2; 465 466 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */ 467 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) && 468 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) { 469 *pos = status->signal; 470 rthdr->it_present |= 471 cpu_to_le32(BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL)); 472 pos++; 473 } 474 475 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */ 476 477 if (!status->chains) { 478 /* IEEE80211_RADIOTAP_ANTENNA */ 479 *pos = status->antenna; 480 pos++; 481 } 482 483 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */ 484 485 /* IEEE80211_RADIOTAP_RX_FLAGS */ 486 /* ensure 2 byte alignment for the 2 byte field as required */ 487 if ((pos - (u8 *)rthdr) & 1) 488 *pos++ = 0; 489 if (status->flag & RX_FLAG_FAILED_PLCP_CRC) 490 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP; 491 put_unaligned_le16(rx_flags, pos); 492 pos += 2; 493 494 if (status->encoding == RX_ENC_HT) { 495 unsigned int stbc; 496 497 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_MCS)); 498 *pos = local->hw.radiotap_mcs_details; 499 if (status->enc_flags & RX_ENC_FLAG_HT_GF) 500 *pos |= IEEE80211_RADIOTAP_MCS_HAVE_FMT; 501 if (status->enc_flags & RX_ENC_FLAG_LDPC) 502 *pos |= IEEE80211_RADIOTAP_MCS_HAVE_FEC; 503 pos++; 504 *pos = 0; 505 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI) 506 *pos |= IEEE80211_RADIOTAP_MCS_SGI; 507 if (status->bw == RATE_INFO_BW_40) 508 *pos |= IEEE80211_RADIOTAP_MCS_BW_40; 509 if (status->enc_flags & RX_ENC_FLAG_HT_GF) 510 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF; 511 if (status->enc_flags & RX_ENC_FLAG_LDPC) 512 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC; 513 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT; 514 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT; 515 pos++; 516 *pos++ = status->rate_idx; 517 } 518 519 if (status->flag & RX_FLAG_AMPDU_DETAILS) { 520 u16 flags = 0; 521 522 /* ensure 4 byte alignment */ 523 while ((pos - (u8 *)rthdr) & 3) 524 pos++; 525 rthdr->it_present |= 526 cpu_to_le32(BIT(IEEE80211_RADIOTAP_AMPDU_STATUS)); 527 put_unaligned_le32(status->ampdu_reference, pos); 528 pos += 4; 529 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN) 530 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN; 531 if (status->flag & RX_FLAG_AMPDU_IS_LAST) 532 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST; 533 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR) 534 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR; 535 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN) 536 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN; 537 if (status->flag & RX_FLAG_AMPDU_EOF_BIT) 538 flags |= IEEE80211_RADIOTAP_AMPDU_EOF; 539 put_unaligned_le16(flags, pos); 540 pos += 2; 541 *pos++ = 0; 542 *pos++ = 0; 543 } 544 545 if (status->encoding == RX_ENC_VHT) { 546 u16 fill = local->hw.radiotap_vht_details; 547 548 /* Leave driver filled fields alone */ 549 fill &= ~le16_to_cpu(vht.known); 550 vht.known |= cpu_to_le16(fill); 551 552 if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_GI && 553 status->enc_flags & RX_ENC_FLAG_SHORT_GI) 554 vht.flags |= IEEE80211_RADIOTAP_VHT_FLAG_SGI; 555 /* in VHT, STBC is binary */ 556 if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_STBC && 557 status->enc_flags & RX_ENC_FLAG_STBC_MASK) 558 vht.flags |= IEEE80211_RADIOTAP_VHT_FLAG_STBC; 559 if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED && 560 status->enc_flags & RX_ENC_FLAG_BF) 561 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED; 562 563 if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) { 564 switch (status->bw) { 565 case RATE_INFO_BW_40: 566 vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_40; 567 break; 568 case RATE_INFO_BW_80: 569 vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_80; 570 break; 571 case RATE_INFO_BW_160: 572 vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_160; 573 break; 574 default: 575 vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_20; 576 break; 577 } 578 } 579 580 /* 581 * If the driver filled in mcs_nss[0], then do not touch it. 582 * 583 * Otherwise, put some information about MCS/NSS into the 584 * user 0 field. Note that this is not technically correct for 585 * an MU frame as we might have decoded a different user. 586 */ 587 if (!vht.mcs_nss[0]) { 588 vht.mcs_nss[0] = (status->rate_idx << 4) | status->nss; 589 590 /* coding field */ 591 if (status->enc_flags & RX_ENC_FLAG_LDPC) 592 vht.coding |= IEEE80211_RADIOTAP_CODING_LDPC_USER0; 593 } 594 595 /* ensure 2 byte alignment */ 596 while ((pos - (u8 *)rthdr) & 1) 597 pos++; 598 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_VHT)); 599 memcpy(pos, &vht, sizeof(vht)); 600 pos += sizeof(vht); 601 } 602 603 if (local->hw.radiotap_timestamp.units_pos >= 0) { 604 u16 accuracy = 0; 605 u8 flags; 606 u64 ts; 607 608 rthdr->it_present |= 609 cpu_to_le32(BIT(IEEE80211_RADIOTAP_TIMESTAMP)); 610 611 /* ensure 8 byte alignment */ 612 while ((pos - (u8 *)rthdr) & 7) 613 pos++; 614 615 if (status->flag & RX_FLAG_MACTIME_IS_RTAP_TS64) { 616 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_64BIT; 617 ts = status->mactime; 618 } else { 619 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT; 620 ts = status->device_timestamp; 621 } 622 623 put_unaligned_le64(ts, pos); 624 pos += sizeof(u64); 625 626 if (local->hw.radiotap_timestamp.accuracy >= 0) { 627 accuracy = local->hw.radiotap_timestamp.accuracy; 628 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY; 629 } 630 put_unaligned_le16(accuracy, pos); 631 pos += sizeof(u16); 632 633 *pos++ = local->hw.radiotap_timestamp.units_pos; 634 *pos++ = flags; 635 } 636 637 if (status->encoding == RX_ENC_HE && 638 status->flag & RX_FLAG_RADIOTAP_HE) { 639 #define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f) 640 641 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) { 642 he.data6 |= HE_PREP(DATA6_NSTS, 643 FIELD_GET(RX_ENC_FLAG_STBC_MASK, 644 status->enc_flags)); 645 he.data3 |= HE_PREP(DATA3_STBC, 1); 646 } else { 647 he.data6 |= HE_PREP(DATA6_NSTS, status->nss); 648 } 649 650 #define CHECK_GI(s) \ 651 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \ 652 (int)NL80211_RATE_INFO_HE_GI_##s) 653 654 CHECK_GI(0_8); 655 CHECK_GI(1_6); 656 CHECK_GI(3_2); 657 658 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx); 659 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm); 660 he.data3 |= HE_PREP(DATA3_CODING, 661 !!(status->enc_flags & RX_ENC_FLAG_LDPC)); 662 663 he.data5 |= HE_PREP(DATA5_GI, status->he_gi); 664 665 switch (status->bw) { 666 case RATE_INFO_BW_20: 667 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC, 668 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ); 669 break; 670 case RATE_INFO_BW_40: 671 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC, 672 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ); 673 break; 674 case RATE_INFO_BW_80: 675 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC, 676 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ); 677 break; 678 case RATE_INFO_BW_160: 679 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC, 680 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ); 681 break; 682 case RATE_INFO_BW_HE_RU: 683 #define CHECK_RU_ALLOC(s) \ 684 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \ 685 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4) 686 687 CHECK_RU_ALLOC(26); 688 CHECK_RU_ALLOC(52); 689 CHECK_RU_ALLOC(106); 690 CHECK_RU_ALLOC(242); 691 CHECK_RU_ALLOC(484); 692 CHECK_RU_ALLOC(996); 693 CHECK_RU_ALLOC(2x996); 694 695 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC, 696 status->he_ru + 4); 697 break; 698 default: 699 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw); 700 } 701 702 /* ensure 2 byte alignment */ 703 while ((pos - (u8 *)rthdr) & 1) 704 pos++; 705 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE)); 706 memcpy(pos, &he, sizeof(he)); 707 pos += sizeof(he); 708 } 709 710 if (status->encoding == RX_ENC_HE && 711 status->flag & RX_FLAG_RADIOTAP_HE_MU) { 712 /* ensure 2 byte alignment */ 713 while ((pos - (u8 *)rthdr) & 1) 714 pos++; 715 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE_MU)); 716 memcpy(pos, &he_mu, sizeof(he_mu)); 717 pos += sizeof(he_mu); 718 } 719 720 if (status->flag & RX_FLAG_NO_PSDU) { 721 rthdr->it_present |= 722 cpu_to_le32(BIT(IEEE80211_RADIOTAP_ZERO_LEN_PSDU)); 723 *pos++ = status->zero_length_psdu_type; 724 } 725 726 if (status->flag & RX_FLAG_RADIOTAP_LSIG) { 727 /* ensure 2 byte alignment */ 728 while ((pos - (u8 *)rthdr) & 1) 729 pos++; 730 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_LSIG)); 731 memcpy(pos, &lsig, sizeof(lsig)); 732 pos += sizeof(lsig); 733 } 734 735 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) { 736 *pos++ = status->chain_signal[chain]; 737 *pos++ = chain; 738 } 739 } 740 741 static struct sk_buff * 742 ieee80211_make_monitor_skb(struct ieee80211_local *local, 743 struct sk_buff **origskb, 744 struct ieee80211_rate *rate, 745 int rtap_space, bool use_origskb) 746 { 747 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb); 748 int rt_hdrlen, needed_headroom; 749 struct sk_buff *skb; 750 751 /* room for the radiotap header based on driver features */ 752 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb); 753 needed_headroom = rt_hdrlen - rtap_space; 754 755 if (use_origskb) { 756 /* only need to expand headroom if necessary */ 757 skb = *origskb; 758 *origskb = NULL; 759 760 /* 761 * This shouldn't trigger often because most devices have an 762 * RX header they pull before we get here, and that should 763 * be big enough for our radiotap information. We should 764 * probably export the length to drivers so that we can have 765 * them allocate enough headroom to start with. 766 */ 767 if (skb_headroom(skb) < needed_headroom && 768 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) { 769 dev_kfree_skb(skb); 770 return NULL; 771 } 772 } else { 773 /* 774 * Need to make a copy and possibly remove radiotap header 775 * and FCS from the original. 776 */ 777 skb = skb_copy_expand(*origskb, needed_headroom + NET_SKB_PAD, 778 0, GFP_ATOMIC); 779 780 if (!skb) 781 return NULL; 782 } 783 784 /* prepend radiotap information */ 785 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true); 786 787 skb_reset_mac_header(skb); 788 skb->ip_summed = CHECKSUM_UNNECESSARY; 789 skb->pkt_type = PACKET_OTHERHOST; 790 skb->protocol = htons(ETH_P_802_2); 791 792 return skb; 793 } 794 795 static bool 796 ieee80211_validate_monitor_radio(struct ieee80211_sub_if_data *sdata, 797 struct ieee80211_local *local, 798 struct ieee80211_rx_status *status) 799 { 800 struct wiphy *wiphy = local->hw.wiphy; 801 int i, freq, bw; 802 803 if (!wiphy->n_radio) 804 return true; 805 806 switch (status->bw) { 807 case RATE_INFO_BW_20: 808 bw = 20000; 809 break; 810 case RATE_INFO_BW_40: 811 bw = 40000; 812 break; 813 case RATE_INFO_BW_80: 814 bw = 80000; 815 break; 816 case RATE_INFO_BW_160: 817 bw = 160000; 818 break; 819 case RATE_INFO_BW_320: 820 bw = 320000; 821 break; 822 default: 823 return false; 824 } 825 826 freq = MHZ_TO_KHZ(status->freq); 827 828 for (i = 0; i < wiphy->n_radio; i++) { 829 if (!(sdata->wdev.radio_mask & BIT(i))) 830 continue; 831 832 if (!ieee80211_radio_freq_range_valid(&wiphy->radio[i], freq, bw)) 833 continue; 834 835 return true; 836 } 837 return false; 838 } 839 840 /* 841 * This function copies a received frame to all monitor interfaces and 842 * returns a cleaned-up SKB that no longer includes the FCS nor the 843 * radiotap header the driver might have added. 844 */ 845 static struct sk_buff * 846 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb, 847 struct ieee80211_rate *rate) 848 { 849 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb); 850 struct ieee80211_sub_if_data *sdata, *prev_sdata = NULL; 851 struct sk_buff *skb, *monskb = NULL; 852 int present_fcs_len = 0; 853 unsigned int rtap_space = 0; 854 struct ieee80211_sub_if_data *monitor_sdata = 855 rcu_dereference(local->monitor_sdata); 856 bool only_monitor = false; 857 unsigned int min_head_len; 858 859 if (WARN_ON_ONCE(status->flag & RX_FLAG_RADIOTAP_TLV_AT_END && 860 !skb_mac_header_was_set(origskb))) { 861 /* with this skb no way to know where frame payload starts */ 862 dev_kfree_skb(origskb); 863 return NULL; 864 } 865 866 if (status->flag & RX_FLAG_RADIOTAP_VHT) 867 rtap_space += sizeof(struct ieee80211_radiotap_vht); 868 869 if (status->flag & RX_FLAG_RADIOTAP_HE) 870 rtap_space += sizeof(struct ieee80211_radiotap_he); 871 872 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) 873 rtap_space += sizeof(struct ieee80211_radiotap_he_mu); 874 875 if (status->flag & RX_FLAG_RADIOTAP_LSIG) 876 rtap_space += sizeof(struct ieee80211_radiotap_lsig); 877 878 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) 879 rtap_space += skb_mac_header(origskb) - &origskb->data[rtap_space]; 880 881 min_head_len = rtap_space; 882 883 /* 884 * First, we may need to make a copy of the skb because 885 * (1) we need to modify it for radiotap (if not present), and 886 * (2) the other RX handlers will modify the skb we got. 887 * 888 * We don't need to, of course, if we aren't going to return 889 * the SKB because it has a bad FCS/PLCP checksum. 890 */ 891 892 if (!(status->flag & RX_FLAG_NO_PSDU)) { 893 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) { 894 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) { 895 /* driver bug */ 896 WARN_ON(1); 897 dev_kfree_skb(origskb); 898 return NULL; 899 } 900 present_fcs_len = FCS_LEN; 901 } 902 903 /* also consider the hdr->frame_control */ 904 min_head_len += 2; 905 } 906 907 /* ensure that the expected data elements are in skb head */ 908 if (!pskb_may_pull(origskb, min_head_len)) { 909 dev_kfree_skb(origskb); 910 return NULL; 911 } 912 913 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space); 914 915 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) { 916 if (only_monitor) { 917 dev_kfree_skb(origskb); 918 return NULL; 919 } 920 921 return ieee80211_clean_skb(origskb, present_fcs_len, 922 rtap_space); 923 } 924 925 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space); 926 927 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) { 928 struct cfg80211_chan_def *chandef; 929 930 chandef = &sdata->vif.bss_conf.chanreq.oper; 931 if (chandef->chan && 932 chandef->chan->center_freq != status->freq) 933 continue; 934 935 if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR) && 936 !ieee80211_validate_monitor_radio(sdata, local, status)) 937 continue; 938 939 if (!prev_sdata) { 940 prev_sdata = sdata; 941 continue; 942 } 943 944 if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) 945 ieee80211_handle_mu_mimo_mon(sdata, origskb, rtap_space); 946 947 if (!monskb) 948 monskb = ieee80211_make_monitor_skb(local, &origskb, 949 rate, rtap_space, 950 false); 951 if (!monskb) 952 continue; 953 954 skb = skb_clone(monskb, GFP_ATOMIC); 955 if (!skb) 956 continue; 957 958 skb->dev = prev_sdata->dev; 959 dev_sw_netstats_rx_add(skb->dev, skb->len); 960 netif_receive_skb(skb); 961 prev_sdata = sdata; 962 } 963 964 if (prev_sdata) { 965 if (monskb) 966 skb = monskb; 967 else 968 skb = ieee80211_make_monitor_skb(local, &origskb, 969 rate, rtap_space, 970 only_monitor); 971 if (skb) { 972 skb->dev = prev_sdata->dev; 973 dev_sw_netstats_rx_add(skb->dev, skb->len); 974 netif_receive_skb(skb); 975 } 976 } 977 978 if (!origskb) 979 return NULL; 980 981 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space); 982 } 983 984 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx) 985 { 986 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 987 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 988 int tid, seqno_idx, security_idx; 989 990 /* does the frame have a qos control field? */ 991 if (ieee80211_is_data_qos(hdr->frame_control)) { 992 u8 *qc = ieee80211_get_qos_ctl(hdr); 993 /* frame has qos control */ 994 tid = *qc & IEEE80211_QOS_CTL_TID_MASK; 995 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT) 996 status->rx_flags |= IEEE80211_RX_AMSDU; 997 998 seqno_idx = tid; 999 security_idx = tid; 1000 } else { 1001 /* 1002 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"): 1003 * 1004 * Sequence numbers for management frames, QoS data 1005 * frames with a broadcast/multicast address in the 1006 * Address 1 field, and all non-QoS data frames sent 1007 * by QoS STAs are assigned using an additional single 1008 * modulo-4096 counter, [...] 1009 * 1010 * We also use that counter for non-QoS STAs. 1011 */ 1012 seqno_idx = IEEE80211_NUM_TIDS; 1013 security_idx = 0; 1014 if (ieee80211_is_mgmt(hdr->frame_control)) 1015 security_idx = IEEE80211_NUM_TIDS; 1016 tid = 0; 1017 } 1018 1019 rx->seqno_idx = seqno_idx; 1020 rx->security_idx = security_idx; 1021 /* Set skb->priority to 1d tag if highest order bit of TID is not set. 1022 * For now, set skb->priority to 0 for other cases. */ 1023 rx->skb->priority = (tid > 7) ? 0 : tid; 1024 } 1025 1026 /** 1027 * DOC: Packet alignment 1028 * 1029 * Drivers always need to pass packets that are aligned to two-byte boundaries 1030 * to the stack. 1031 * 1032 * Additionally, they should, if possible, align the payload data in a way that 1033 * guarantees that the contained IP header is aligned to a four-byte 1034 * boundary. In the case of regular frames, this simply means aligning the 1035 * payload to a four-byte boundary (because either the IP header is directly 1036 * contained, or IV/RFC1042 headers that have a length divisible by four are 1037 * in front of it). If the payload data is not properly aligned and the 1038 * architecture doesn't support efficient unaligned operations, mac80211 1039 * will align the data. 1040 * 1041 * With A-MSDU frames, however, the payload data address must yield two modulo 1042 * four because there are 14-byte 802.3 headers within the A-MSDU frames that 1043 * push the IP header further back to a multiple of four again. Thankfully, the 1044 * specs were sane enough this time around to require padding each A-MSDU 1045 * subframe to a length that is a multiple of four. 1046 * 1047 * Padding like Atheros hardware adds which is between the 802.11 header and 1048 * the payload is not supported; the driver is required to move the 802.11 1049 * header to be directly in front of the payload in that case. 1050 */ 1051 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx) 1052 { 1053 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 1054 WARN_ON_ONCE((unsigned long)rx->skb->data & 1); 1055 #endif 1056 } 1057 1058 1059 /* rx handlers */ 1060 1061 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb) 1062 { 1063 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 1064 1065 if (is_multicast_ether_addr(hdr->addr1)) 1066 return 0; 1067 1068 return ieee80211_is_robust_mgmt_frame(skb); 1069 } 1070 1071 1072 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb) 1073 { 1074 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 1075 1076 if (!is_multicast_ether_addr(hdr->addr1)) 1077 return 0; 1078 1079 return ieee80211_is_robust_mgmt_frame(skb); 1080 } 1081 1082 1083 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */ 1084 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb) 1085 { 1086 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data; 1087 struct ieee80211_mmie *mmie; 1088 struct ieee80211_mmie_16 *mmie16; 1089 1090 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da)) 1091 return -1; 1092 1093 if (!ieee80211_is_robust_mgmt_frame(skb) && 1094 !ieee80211_is_beacon(hdr->frame_control)) 1095 return -1; /* not a robust management frame */ 1096 1097 mmie = (struct ieee80211_mmie *) 1098 (skb->data + skb->len - sizeof(*mmie)); 1099 if (mmie->element_id == WLAN_EID_MMIE && 1100 mmie->length == sizeof(*mmie) - 2) 1101 return le16_to_cpu(mmie->key_id); 1102 1103 mmie16 = (struct ieee80211_mmie_16 *) 1104 (skb->data + skb->len - sizeof(*mmie16)); 1105 if (skb->len >= 24 + sizeof(*mmie16) && 1106 mmie16->element_id == WLAN_EID_MMIE && 1107 mmie16->length == sizeof(*mmie16) - 2) 1108 return le16_to_cpu(mmie16->key_id); 1109 1110 return -1; 1111 } 1112 1113 static int ieee80211_get_keyid(struct sk_buff *skb) 1114 { 1115 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 1116 __le16 fc = hdr->frame_control; 1117 int hdrlen = ieee80211_hdrlen(fc); 1118 u8 keyid; 1119 1120 /* WEP, TKIP, CCMP and GCMP */ 1121 if (unlikely(skb->len < hdrlen + IEEE80211_WEP_IV_LEN)) 1122 return -EINVAL; 1123 1124 skb_copy_bits(skb, hdrlen + 3, &keyid, 1); 1125 1126 keyid >>= 6; 1127 1128 return keyid; 1129 } 1130 1131 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx) 1132 { 1133 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 1134 char *dev_addr = rx->sdata->vif.addr; 1135 1136 if (ieee80211_is_data(hdr->frame_control)) { 1137 if (is_multicast_ether_addr(hdr->addr1)) { 1138 if (ieee80211_has_tods(hdr->frame_control) || 1139 !ieee80211_has_fromds(hdr->frame_control)) 1140 return RX_DROP_U_MESH_DS_BITS; 1141 if (ether_addr_equal(hdr->addr3, dev_addr)) 1142 return RX_DROP_U_MESH_A3_MISMATCH; 1143 } else { 1144 if (!ieee80211_has_a4(hdr->frame_control)) 1145 return RX_DROP_U_MESH_NO_A4; 1146 if (ether_addr_equal(hdr->addr4, dev_addr)) 1147 return RX_DROP_U_MESH_A4_MISMATCH; 1148 } 1149 } 1150 1151 /* If there is not an established peer link and this is not a peer link 1152 * establisment frame, beacon or probe, drop the frame. 1153 */ 1154 1155 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) { 1156 struct ieee80211_mgmt *mgmt; 1157 1158 if (!ieee80211_is_mgmt(hdr->frame_control)) 1159 return RX_DROP_U_MESH_UNEXP_DATA; 1160 1161 if (ieee80211_is_action(hdr->frame_control)) { 1162 u8 category; 1163 1164 /* make sure category field is present */ 1165 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(category)) 1166 return RX_DROP_U_RUNT_ACTION; 1167 1168 mgmt = (struct ieee80211_mgmt *)hdr; 1169 category = mgmt->u.action.category; 1170 if (category != WLAN_CATEGORY_MESH_ACTION && 1171 category != WLAN_CATEGORY_SELF_PROTECTED) 1172 return RX_DROP_U_MESH_WRONG_ACTION; 1173 return RX_CONTINUE; 1174 } 1175 1176 if (ieee80211_is_probe_req(hdr->frame_control) || 1177 ieee80211_is_probe_resp(hdr->frame_control) || 1178 ieee80211_is_beacon(hdr->frame_control) || 1179 ieee80211_is_auth(hdr->frame_control)) 1180 return RX_CONTINUE; 1181 1182 return RX_DROP_U_MESH_UNEXP_MGMT; 1183 } 1184 1185 return RX_CONTINUE; 1186 } 1187 1188 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx, 1189 int index) 1190 { 1191 struct sk_buff_head *frames = &tid_agg_rx->reorder[index].buf; 1192 struct sk_buff *tail = skb_peek_tail(frames); 1193 struct ieee80211_rx_status *status; 1194 1195 if (tid_agg_rx->reorder_buf_filtered && 1196 tid_agg_rx->reorder_buf_filtered & BIT_ULL(index)) 1197 return true; 1198 1199 if (!tail) 1200 return false; 1201 1202 status = IEEE80211_SKB_RXCB(tail); 1203 if (status->flag & RX_FLAG_AMSDU_MORE) 1204 return false; 1205 1206 return true; 1207 } 1208 1209 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata, 1210 struct tid_ampdu_rx *tid_agg_rx, 1211 int index, 1212 struct sk_buff_head *frames) 1213 { 1214 struct sk_buff_head *skb_list = &tid_agg_rx->reorder[index].buf; 1215 struct sk_buff *skb; 1216 struct ieee80211_rx_status *status; 1217 1218 lockdep_assert_held(&tid_agg_rx->reorder_lock); 1219 1220 if (skb_queue_empty(skb_list)) 1221 goto no_frame; 1222 1223 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) { 1224 __skb_queue_purge(skb_list); 1225 goto no_frame; 1226 } 1227 1228 /* release frames from the reorder ring buffer */ 1229 tid_agg_rx->stored_mpdu_num--; 1230 while ((skb = __skb_dequeue(skb_list))) { 1231 status = IEEE80211_SKB_RXCB(skb); 1232 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE; 1233 __skb_queue_tail(frames, skb); 1234 } 1235 1236 no_frame: 1237 if (tid_agg_rx->reorder_buf_filtered) 1238 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index); 1239 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num); 1240 } 1241 1242 static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata, 1243 struct tid_ampdu_rx *tid_agg_rx, 1244 u16 head_seq_num, 1245 struct sk_buff_head *frames) 1246 { 1247 int index; 1248 1249 lockdep_assert_held(&tid_agg_rx->reorder_lock); 1250 1251 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) { 1252 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size; 1253 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index, 1254 frames); 1255 } 1256 } 1257 1258 /* 1259 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If 1260 * the skb was added to the buffer longer than this time ago, the earlier 1261 * frames that have not yet been received are assumed to be lost and the skb 1262 * can be released for processing. This may also release other skb's from the 1263 * reorder buffer if there are no additional gaps between the frames. 1264 * 1265 * Callers must hold tid_agg_rx->reorder_lock. 1266 */ 1267 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10) 1268 1269 static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata, 1270 struct tid_ampdu_rx *tid_agg_rx, 1271 struct sk_buff_head *frames) 1272 { 1273 int index, i, j; 1274 1275 lockdep_assert_held(&tid_agg_rx->reorder_lock); 1276 1277 /* release the buffer until next missing frame */ 1278 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size; 1279 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) && 1280 tid_agg_rx->stored_mpdu_num) { 1281 /* 1282 * No buffers ready to be released, but check whether any 1283 * frames in the reorder buffer have timed out. 1284 */ 1285 int skipped = 1; 1286 for (j = (index + 1) % tid_agg_rx->buf_size; j != index; 1287 j = (j + 1) % tid_agg_rx->buf_size) { 1288 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) { 1289 skipped++; 1290 continue; 1291 } 1292 if (skipped && 1293 !time_after(jiffies, tid_agg_rx->reorder[j].time + 1294 HT_RX_REORDER_BUF_TIMEOUT)) 1295 goto set_release_timer; 1296 1297 /* don't leave incomplete A-MSDUs around */ 1298 for (i = (index + 1) % tid_agg_rx->buf_size; i != j; 1299 i = (i + 1) % tid_agg_rx->buf_size) 1300 __skb_queue_purge(&tid_agg_rx->reorder[i].buf); 1301 1302 ht_dbg_ratelimited(sdata, 1303 "release an RX reorder frame due to timeout on earlier frames\n"); 1304 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j, 1305 frames); 1306 1307 /* 1308 * Increment the head seq# also for the skipped slots. 1309 */ 1310 tid_agg_rx->head_seq_num = 1311 (tid_agg_rx->head_seq_num + 1312 skipped) & IEEE80211_SN_MASK; 1313 skipped = 0; 1314 } 1315 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) { 1316 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index, 1317 frames); 1318 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size; 1319 } 1320 1321 if (tid_agg_rx->stored_mpdu_num) { 1322 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size; 1323 1324 for (; j != (index - 1) % tid_agg_rx->buf_size; 1325 j = (j + 1) % tid_agg_rx->buf_size) { 1326 if (ieee80211_rx_reorder_ready(tid_agg_rx, j)) 1327 break; 1328 } 1329 1330 set_release_timer: 1331 1332 if (!tid_agg_rx->removed) 1333 mod_timer(&tid_agg_rx->reorder_timer, 1334 tid_agg_rx->reorder[j].time + 1 + 1335 HT_RX_REORDER_BUF_TIMEOUT); 1336 } else { 1337 timer_delete(&tid_agg_rx->reorder_timer); 1338 } 1339 } 1340 1341 /* 1342 * As this function belongs to the RX path it must be under 1343 * rcu_read_lock protection. It returns false if the frame 1344 * can be processed immediately, true if it was consumed. 1345 */ 1346 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata, 1347 struct tid_ampdu_rx *tid_agg_rx, 1348 struct sk_buff *skb, 1349 struct sk_buff_head *frames) 1350 { 1351 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 1352 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 1353 u16 mpdu_seq_num = ieee80211_get_sn(hdr); 1354 u16 head_seq_num, buf_size; 1355 int index; 1356 bool ret = true; 1357 1358 spin_lock(&tid_agg_rx->reorder_lock); 1359 1360 /* 1361 * Offloaded BA sessions have no known starting sequence number so pick 1362 * one from first Rxed frame for this tid after BA was started. 1363 */ 1364 if (unlikely(tid_agg_rx->auto_seq)) { 1365 tid_agg_rx->auto_seq = false; 1366 tid_agg_rx->ssn = mpdu_seq_num; 1367 tid_agg_rx->head_seq_num = mpdu_seq_num; 1368 } 1369 1370 buf_size = tid_agg_rx->buf_size; 1371 head_seq_num = tid_agg_rx->head_seq_num; 1372 1373 /* 1374 * If the current MPDU's SN is smaller than the SSN, it shouldn't 1375 * be reordered. 1376 */ 1377 if (unlikely(!tid_agg_rx->started)) { 1378 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) { 1379 ret = false; 1380 goto out; 1381 } 1382 tid_agg_rx->started = true; 1383 } 1384 1385 /* frame with out of date sequence number */ 1386 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) { 1387 dev_kfree_skb(skb); 1388 goto out; 1389 } 1390 1391 /* 1392 * If frame the sequence number exceeds our buffering window 1393 * size release some previous frames to make room for this one. 1394 */ 1395 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) { 1396 head_seq_num = ieee80211_sn_inc( 1397 ieee80211_sn_sub(mpdu_seq_num, buf_size)); 1398 /* release stored frames up to new head to stack */ 1399 ieee80211_release_reorder_frames(sdata, tid_agg_rx, 1400 head_seq_num, frames); 1401 } 1402 1403 /* Now the new frame is always in the range of the reordering buffer */ 1404 1405 index = mpdu_seq_num % tid_agg_rx->buf_size; 1406 1407 /* check if we already stored this frame */ 1408 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) { 1409 dev_kfree_skb(skb); 1410 goto out; 1411 } 1412 1413 /* 1414 * If the current MPDU is in the right order and nothing else 1415 * is stored we can process it directly, no need to buffer it. 1416 * If it is first but there's something stored, we may be able 1417 * to release frames after this one. 1418 */ 1419 if (mpdu_seq_num == tid_agg_rx->head_seq_num && 1420 tid_agg_rx->stored_mpdu_num == 0) { 1421 if (!(status->flag & RX_FLAG_AMSDU_MORE)) 1422 tid_agg_rx->head_seq_num = 1423 ieee80211_sn_inc(tid_agg_rx->head_seq_num); 1424 ret = false; 1425 goto out; 1426 } 1427 1428 /* put the frame in the reordering buffer */ 1429 __skb_queue_tail(&tid_agg_rx->reorder[index].buf, skb); 1430 if (!(status->flag & RX_FLAG_AMSDU_MORE)) { 1431 tid_agg_rx->reorder[index].time = jiffies; 1432 tid_agg_rx->stored_mpdu_num++; 1433 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames); 1434 } 1435 1436 out: 1437 spin_unlock(&tid_agg_rx->reorder_lock); 1438 return ret; 1439 } 1440 1441 /* 1442 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns 1443 * true if the MPDU was buffered, false if it should be processed. 1444 */ 1445 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx, 1446 struct sk_buff_head *frames) 1447 { 1448 struct sk_buff *skb = rx->skb; 1449 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 1450 struct sta_info *sta = rx->sta; 1451 struct tid_ampdu_rx *tid_agg_rx; 1452 u16 sc; 1453 u8 tid, ack_policy; 1454 1455 if (!ieee80211_is_data_qos(hdr->frame_control) || 1456 is_multicast_ether_addr(hdr->addr1)) 1457 goto dont_reorder; 1458 1459 /* 1460 * filter the QoS data rx stream according to 1461 * STA/TID and check if this STA/TID is on aggregation 1462 */ 1463 1464 if (!sta) 1465 goto dont_reorder; 1466 1467 ack_policy = *ieee80211_get_qos_ctl(hdr) & 1468 IEEE80211_QOS_CTL_ACK_POLICY_MASK; 1469 tid = ieee80211_get_tid(hdr); 1470 1471 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]); 1472 if (!tid_agg_rx) { 1473 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK && 1474 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) && 1475 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg)) 1476 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid, 1477 WLAN_BACK_RECIPIENT, 1478 WLAN_REASON_QSTA_REQUIRE_SETUP, 1479 ieee80211_s1g_use_ndp_ba(rx->sdata, 1480 rx->sta)); 1481 goto dont_reorder; 1482 } 1483 1484 /* qos null data frames are excluded */ 1485 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC))) 1486 goto dont_reorder; 1487 1488 /* not part of a BA session */ 1489 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_NOACK) 1490 goto dont_reorder; 1491 1492 /* new, potentially un-ordered, ampdu frame - process it */ 1493 1494 /* reset session timer */ 1495 if (tid_agg_rx->timeout) 1496 tid_agg_rx->last_rx = jiffies; 1497 1498 /* if this mpdu is fragmented - terminate rx aggregation session */ 1499 sc = le16_to_cpu(hdr->seq_ctrl); 1500 if (sc & IEEE80211_SCTL_FRAG) { 1501 ieee80211_queue_skb_to_iface(rx->sdata, rx->link_id, NULL, skb); 1502 return; 1503 } 1504 1505 /* 1506 * No locking needed -- we will only ever process one 1507 * RX packet at a time, and thus own tid_agg_rx. All 1508 * other code manipulating it needs to (and does) make 1509 * sure that we cannot get to it any more before doing 1510 * anything with it. 1511 */ 1512 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb, 1513 frames)) 1514 return; 1515 1516 dont_reorder: 1517 __skb_queue_tail(frames, skb); 1518 } 1519 1520 static ieee80211_rx_result debug_noinline 1521 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx) 1522 { 1523 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 1524 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 1525 1526 if (status->flag & RX_FLAG_DUP_VALIDATED) 1527 return RX_CONTINUE; 1528 1529 if (ieee80211_is_ext(hdr->frame_control)) 1530 return RX_CONTINUE; 1531 1532 /* 1533 * Drop duplicate 802.11 retransmissions 1534 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery") 1535 */ 1536 1537 if (rx->skb->len < 24) 1538 return RX_CONTINUE; 1539 1540 if (ieee80211_is_ctl(hdr->frame_control) || 1541 ieee80211_is_any_nullfunc(hdr->frame_control)) 1542 return RX_CONTINUE; 1543 1544 if (!rx->sta) 1545 return RX_CONTINUE; 1546 1547 if (unlikely(is_multicast_ether_addr(hdr->addr1))) { 1548 struct ieee80211_sub_if_data *sdata = rx->sdata; 1549 u16 sn = ieee80211_get_sn(hdr); 1550 1551 if (!ieee80211_is_data_present(hdr->frame_control)) 1552 return RX_CONTINUE; 1553 1554 if (!ieee80211_vif_is_mld(&sdata->vif) || 1555 sdata->vif.type != NL80211_IFTYPE_STATION) 1556 return RX_CONTINUE; 1557 1558 if (sdata->u.mgd.mcast_seq_last != IEEE80211_SN_MODULO && 1559 ieee80211_sn_less_eq(sn, sdata->u.mgd.mcast_seq_last)) 1560 return RX_DROP_U_DUP; 1561 1562 sdata->u.mgd.mcast_seq_last = sn; 1563 return RX_CONTINUE; 1564 } 1565 1566 if (unlikely(ieee80211_has_retry(hdr->frame_control) && 1567 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) { 1568 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount); 1569 rx->link_sta->rx_stats.num_duplicates++; 1570 return RX_DROP_U_DUP; 1571 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) { 1572 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl; 1573 } 1574 1575 return RX_CONTINUE; 1576 } 1577 1578 static ieee80211_rx_result debug_noinline 1579 ieee80211_rx_h_check(struct ieee80211_rx_data *rx) 1580 { 1581 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 1582 1583 /* Drop disallowed frame classes based on STA auth/assoc state; 1584 * IEEE 802.11, Chap 5.5. 1585 * 1586 * mac80211 filters only based on association state, i.e. it drops 1587 * Class 3 frames from not associated stations. hostapd sends 1588 * deauth/disassoc frames when needed. In addition, hostapd is 1589 * responsible for filtering on both auth and assoc states. 1590 */ 1591 1592 if (ieee80211_vif_is_mesh(&rx->sdata->vif)) 1593 return ieee80211_rx_mesh_check(rx); 1594 1595 /* 1596 * Wi-Fi Aware (TM) 4.0 specification 6.2.5: 1597 * For NAN_DATA, unicast data frames must have A2 (source) 1598 * assigned to an active NDP. If not the frame must be dropped 1599 * and NAN Data Path termination frame should be sent. Notify 1600 * user space so it can do so. 1601 */ 1602 if (rx->sdata->vif.type == NL80211_IFTYPE_NAN_DATA) { 1603 if (ieee80211_is_data(hdr->frame_control) && 1604 !is_multicast_ether_addr(hdr->addr1) && 1605 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC))) { 1606 if (cfg80211_rx_spurious_frame(rx->sdata->dev, hdr->addr2, 1607 rx->link_id, GFP_ATOMIC)) 1608 return RX_DROP_U_SPURIOUS_NOTIF; 1609 return RX_DROP_U_SPURIOUS; 1610 } 1611 return RX_CONTINUE; 1612 } 1613 1614 if (unlikely((ieee80211_is_data(hdr->frame_control) || 1615 ieee80211_is_pspoll(hdr->frame_control)) && 1616 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC && 1617 rx->sdata->vif.type != NL80211_IFTYPE_OCB && 1618 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) { 1619 /* 1620 * accept port control frames from the AP even when it's not 1621 * yet marked ASSOC to prevent a race where we don't set the 1622 * assoc bit quickly enough before it sends the first frame 1623 */ 1624 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION && 1625 ieee80211_is_data_present(hdr->frame_control)) { 1626 unsigned int hdrlen; 1627 __be16 ethertype; 1628 1629 hdrlen = ieee80211_hdrlen(hdr->frame_control); 1630 1631 if (rx->skb->len < hdrlen + 8) 1632 return RX_DROP_U_RUNT_DATA; 1633 1634 skb_copy_bits(rx->skb, hdrlen + 6, ðertype, 2); 1635 if (ethertype == rx->sdata->control_port_protocol) 1636 return RX_CONTINUE; 1637 } 1638 1639 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && 1640 cfg80211_rx_spurious_frame(rx->sdata->dev, hdr->addr2, 1641 rx->link_id, GFP_ATOMIC)) 1642 return RX_DROP_U_SPURIOUS_NOTIF; 1643 1644 return RX_DROP_U_SPURIOUS; 1645 } 1646 1647 return RX_CONTINUE; 1648 } 1649 1650 1651 static ieee80211_rx_result debug_noinline 1652 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx) 1653 { 1654 struct ieee80211_local *local; 1655 struct ieee80211_hdr *hdr; 1656 struct sk_buff *skb; 1657 1658 local = rx->local; 1659 skb = rx->skb; 1660 hdr = (struct ieee80211_hdr *) skb->data; 1661 1662 if (!local->pspolling) 1663 return RX_CONTINUE; 1664 1665 if (!ieee80211_has_fromds(hdr->frame_control)) 1666 /* this is not from AP */ 1667 return RX_CONTINUE; 1668 1669 if (!ieee80211_is_data(hdr->frame_control)) 1670 return RX_CONTINUE; 1671 1672 if (!ieee80211_has_moredata(hdr->frame_control)) { 1673 /* AP has no more frames buffered for us */ 1674 local->pspolling = false; 1675 return RX_CONTINUE; 1676 } 1677 1678 /* more data bit is set, let's request a new frame from the AP */ 1679 ieee80211_send_pspoll(local, rx->sdata); 1680 1681 return RX_CONTINUE; 1682 } 1683 1684 static void sta_ps_start(struct sta_info *sta) 1685 { 1686 struct ieee80211_sub_if_data *sdata = sta->sdata; 1687 struct ieee80211_local *local = sdata->local; 1688 struct ps_data *ps; 1689 int tid; 1690 1691 if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 1692 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1693 ps = &sdata->bss->ps; 1694 else 1695 return; 1696 1697 atomic_inc(&ps->num_sta_ps); 1698 set_sta_flag(sta, WLAN_STA_PS_STA); 1699 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS)) 1700 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta); 1701 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n", 1702 sta->sta.addr, sta->sta.aid); 1703 1704 ieee80211_clear_fast_xmit(sta); 1705 1706 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) { 1707 struct ieee80211_txq *txq = sta->sta.txq[tid]; 1708 struct txq_info *txqi = to_txq_info(txq); 1709 1710 spin_lock(&local->active_txq_lock[txq->ac]); 1711 if (!list_empty(&txqi->schedule_order)) 1712 list_del_init(&txqi->schedule_order); 1713 spin_unlock(&local->active_txq_lock[txq->ac]); 1714 1715 if (txq_has_queue(txq)) 1716 set_bit(tid, &sta->txq_buffered_tids); 1717 else 1718 clear_bit(tid, &sta->txq_buffered_tids); 1719 } 1720 } 1721 1722 static void sta_ps_end(struct sta_info *sta) 1723 { 1724 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n", 1725 sta->sta.addr, sta->sta.aid); 1726 1727 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) { 1728 /* 1729 * Clear the flag only if the other one is still set 1730 * so that the TX path won't start TX'ing new frames 1731 * directly ... In the case that the driver flag isn't 1732 * set ieee80211_sta_ps_deliver_wakeup() will clear it. 1733 */ 1734 clear_sta_flag(sta, WLAN_STA_PS_STA); 1735 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n", 1736 sta->sta.addr, sta->sta.aid); 1737 return; 1738 } 1739 1740 set_sta_flag(sta, WLAN_STA_PS_DELIVER); 1741 clear_sta_flag(sta, WLAN_STA_PS_STA); 1742 ieee80211_sta_ps_deliver_wakeup(sta); 1743 } 1744 1745 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start) 1746 { 1747 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1748 bool in_ps; 1749 1750 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS)); 1751 1752 /* Don't let the same PS state be set twice */ 1753 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA); 1754 if ((start && in_ps) || (!start && !in_ps)) 1755 return -EINVAL; 1756 1757 if (start) 1758 sta_ps_start(sta); 1759 else 1760 sta_ps_end(sta); 1761 1762 return 0; 1763 } 1764 EXPORT_SYMBOL(ieee80211_sta_ps_transition); 1765 1766 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta) 1767 { 1768 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1769 1770 if (test_sta_flag(sta, WLAN_STA_SP)) 1771 return; 1772 1773 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1774 ieee80211_sta_ps_deliver_poll_response(sta); 1775 else 1776 set_sta_flag(sta, WLAN_STA_PSPOLL); 1777 } 1778 EXPORT_SYMBOL(ieee80211_sta_pspoll); 1779 1780 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid) 1781 { 1782 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1783 int ac = ieee80211_ac_from_tid(tid); 1784 1785 /* 1786 * If this AC is not trigger-enabled do nothing unless the 1787 * driver is calling us after it already checked. 1788 * 1789 * NB: This could/should check a separate bitmap of trigger- 1790 * enabled queues, but for now we only implement uAPSD w/o 1791 * TSPEC changes to the ACs, so they're always the same. 1792 */ 1793 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) && 1794 tid != IEEE80211_NUM_TIDS) 1795 return; 1796 1797 /* if we are in a service period, do nothing */ 1798 if (test_sta_flag(sta, WLAN_STA_SP)) 1799 return; 1800 1801 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1802 ieee80211_sta_ps_deliver_uapsd(sta); 1803 else 1804 set_sta_flag(sta, WLAN_STA_UAPSD); 1805 } 1806 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger); 1807 1808 static ieee80211_rx_result debug_noinline 1809 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx) 1810 { 1811 struct ieee80211_sub_if_data *sdata = rx->sdata; 1812 struct ieee80211_hdr *hdr = (void *)rx->skb->data; 1813 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 1814 1815 if (!rx->sta) 1816 return RX_CONTINUE; 1817 1818 if (sdata->vif.type != NL80211_IFTYPE_AP && 1819 sdata->vif.type != NL80211_IFTYPE_AP_VLAN) 1820 return RX_CONTINUE; 1821 1822 /* 1823 * The device handles station powersave, so don't do anything about 1824 * uAPSD and PS-Poll frames (the latter shouldn't even come up from 1825 * it to mac80211 since they're handled.) 1826 */ 1827 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS)) 1828 return RX_CONTINUE; 1829 1830 /* 1831 * Don't do anything if the station isn't already asleep. In 1832 * the uAPSD case, the station will probably be marked asleep, 1833 * in the PS-Poll case the station must be confused ... 1834 */ 1835 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA)) 1836 return RX_CONTINUE; 1837 1838 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) { 1839 ieee80211_sta_pspoll(&rx->sta->sta); 1840 1841 /* Free PS Poll skb here instead of returning RX_DROP that would 1842 * count as an dropped frame. */ 1843 dev_kfree_skb(rx->skb); 1844 1845 return RX_QUEUED; 1846 } else if (!ieee80211_has_morefrags(hdr->frame_control) && 1847 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) && 1848 ieee80211_has_pm(hdr->frame_control) && 1849 (ieee80211_is_data_qos(hdr->frame_control) || 1850 ieee80211_is_qos_nullfunc(hdr->frame_control))) { 1851 u8 tid = ieee80211_get_tid(hdr); 1852 1853 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid); 1854 } 1855 1856 return RX_CONTINUE; 1857 } 1858 1859 static ieee80211_rx_result debug_noinline 1860 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx) 1861 { 1862 struct sta_info *sta = rx->sta; 1863 struct link_sta_info *link_sta = rx->link_sta; 1864 struct sk_buff *skb = rx->skb; 1865 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 1866 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 1867 int i; 1868 1869 if (!sta || !link_sta) 1870 return RX_CONTINUE; 1871 1872 /* 1873 * Update last_rx only for IBSS packets which are for the current 1874 * BSSID and for station already AUTHORIZED to avoid keeping the 1875 * current IBSS network alive in cases where other STAs start 1876 * using different BSSID. This will also give the station another 1877 * chance to restart the authentication/authorization in case 1878 * something went wrong the first time. 1879 */ 1880 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) { 1881 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len, 1882 NL80211_IFTYPE_ADHOC); 1883 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) && 1884 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) { 1885 link_sta->rx_stats.last_rx = jiffies; 1886 if (ieee80211_is_data_present(hdr->frame_control) && 1887 !is_multicast_ether_addr(hdr->addr1)) 1888 link_sta->rx_stats.last_rate = 1889 sta_stats_encode_rate(status); 1890 } 1891 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) { 1892 link_sta->rx_stats.last_rx = jiffies; 1893 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) && 1894 !is_multicast_ether_addr(hdr->addr1)) { 1895 /* 1896 * Mesh beacons will update last_rx when if they are found to 1897 * match the current local configuration when processed. 1898 */ 1899 link_sta->rx_stats.last_rx = jiffies; 1900 if (ieee80211_is_data_present(hdr->frame_control)) 1901 link_sta->rx_stats.last_rate = sta_stats_encode_rate(status); 1902 } 1903 1904 link_sta->rx_stats.fragments++; 1905 1906 u64_stats_update_begin(&link_sta->rx_stats.syncp); 1907 u64_stats_add(&link_sta->rx_stats.bytes, rx->skb->len); 1908 u64_stats_update_end(&link_sta->rx_stats.syncp); 1909 1910 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) { 1911 link_sta->rx_stats.last_signal = status->signal; 1912 ewma_signal_add(&link_sta->rx_stats_avg.signal, 1913 -status->signal); 1914 } 1915 1916 if (status->chains) { 1917 link_sta->rx_stats.chains = status->chains; 1918 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) { 1919 int signal = status->chain_signal[i]; 1920 1921 if (!(status->chains & BIT(i))) 1922 continue; 1923 1924 link_sta->rx_stats.chain_signal_last[i] = signal; 1925 ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i], 1926 -signal); 1927 } 1928 } 1929 1930 if (ieee80211_is_s1g_beacon(hdr->frame_control)) 1931 return RX_CONTINUE; 1932 1933 /* 1934 * Change STA power saving mode only at the end of a frame 1935 * exchange sequence, and only for a data or management 1936 * frame as specified in IEEE 802.11-2016 11.2.3.2 1937 */ 1938 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) && 1939 !ieee80211_has_morefrags(hdr->frame_control) && 1940 !is_multicast_ether_addr(hdr->addr1) && 1941 (ieee80211_is_mgmt(hdr->frame_control) || 1942 ieee80211_is_data(hdr->frame_control)) && 1943 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) && 1944 (rx->sdata->vif.type == NL80211_IFTYPE_AP || 1945 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) { 1946 if (test_sta_flag(sta, WLAN_STA_PS_STA)) { 1947 if (!ieee80211_has_pm(hdr->frame_control)) 1948 sta_ps_end(sta); 1949 } else { 1950 if (ieee80211_has_pm(hdr->frame_control)) 1951 sta_ps_start(sta); 1952 } 1953 } 1954 1955 /* mesh power save support */ 1956 if (ieee80211_vif_is_mesh(&rx->sdata->vif)) 1957 ieee80211_mps_rx_h_sta_process(sta, hdr); 1958 1959 /* 1960 * Drop (qos-)data::nullfunc frames silently, since they 1961 * are used only to control station power saving mode. 1962 */ 1963 if (ieee80211_is_any_nullfunc(hdr->frame_control)) { 1964 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc); 1965 1966 /* 1967 * If we receive a 4-addr nullfunc frame from a STA 1968 * that was not moved to a 4-addr STA vlan yet send 1969 * the event to userspace and for older hostapd drop 1970 * the frame to the monitor interface. 1971 */ 1972 if (ieee80211_has_a4(hdr->frame_control) && 1973 (rx->sdata->vif.type == NL80211_IFTYPE_AP || 1974 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 1975 !rx->sdata->u.vlan.sta))) { 1976 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT)) 1977 cfg80211_rx_unexpected_4addr_frame( 1978 rx->sdata->dev, sta->sta.addr, 1979 rx->link_id, GFP_ATOMIC); 1980 return RX_DROP_U_UNEXPECTED_4ADDR_FRAME; 1981 } 1982 /* 1983 * Update counter and free packet here to avoid 1984 * counting this as a dropped packed. 1985 */ 1986 link_sta->rx_stats.packets++; 1987 dev_kfree_skb(rx->skb); 1988 return RX_QUEUED; 1989 } 1990 1991 return RX_CONTINUE; 1992 } /* ieee80211_rx_h_sta_process */ 1993 1994 static struct ieee80211_key * 1995 ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx) 1996 { 1997 struct ieee80211_key *key = NULL; 1998 int idx2; 1999 2000 /* Make sure key gets set if either BIGTK key index is set so that 2001 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected 2002 * Beacon frames and Beacon frames that claim to use another BIGTK key 2003 * index (i.e., a key that we do not have). 2004 */ 2005 2006 if (idx < 0) { 2007 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS; 2008 idx2 = idx + 1; 2009 } else { 2010 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) 2011 idx2 = idx + 1; 2012 else 2013 idx2 = idx - 1; 2014 } 2015 2016 if (rx->link_sta) 2017 key = rcu_dereference(rx->link_sta->gtk[idx]); 2018 if (!key) 2019 key = rcu_dereference(rx->link->gtk[idx]); 2020 if (!key && rx->link_sta) 2021 key = rcu_dereference(rx->link_sta->gtk[idx2]); 2022 if (!key) 2023 key = rcu_dereference(rx->link->gtk[idx2]); 2024 2025 return key; 2026 } 2027 2028 static ieee80211_rx_result debug_noinline 2029 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx) 2030 { 2031 struct sk_buff *skb = rx->skb; 2032 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 2033 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 2034 int keyidx; 2035 ieee80211_rx_result result = RX_DROP_U_DECRYPT_FAIL; 2036 struct ieee80211_key *sta_ptk = NULL; 2037 struct ieee80211_key *ptk_idx = NULL; 2038 int mmie_keyidx = -1; 2039 __le16 fc; 2040 2041 if (ieee80211_is_ext(hdr->frame_control)) 2042 return RX_CONTINUE; 2043 2044 /* 2045 * Key selection 101 2046 * 2047 * There are five types of keys: 2048 * - GTK (group keys) 2049 * - IGTK (group keys for management frames) 2050 * - BIGTK (group keys for Beacon frames) 2051 * - PTK (pairwise keys) 2052 * - STK (station-to-station pairwise keys) 2053 * 2054 * When selecting a key, we have to distinguish between multicast 2055 * (including broadcast) and unicast frames, the latter can only 2056 * use PTKs and STKs while the former always use GTKs, IGTKs, and 2057 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used, 2058 * then unicast frames can also use key indices like GTKs. Hence, if we 2059 * don't have a PTK/STK we check the key index for a WEP key. 2060 * 2061 * Note that in a regular BSS, multicast frames are sent by the 2062 * AP only, associated stations unicast the frame to the AP first 2063 * which then multicasts it on their behalf. 2064 * 2065 * There is also a slight problem in IBSS mode: GTKs are negotiated 2066 * with each station, that is something we don't currently handle. 2067 * The spec seems to expect that one negotiates the same key with 2068 * every station but there's no such requirement; VLANs could be 2069 * possible. 2070 */ 2071 2072 /* start without a key */ 2073 rx->key = NULL; 2074 fc = hdr->frame_control; 2075 2076 if (rx->sta) { 2077 int keyid = rx->sta->ptk_idx; 2078 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]); 2079 2080 if (ieee80211_has_protected(fc) && 2081 !(status->flag & RX_FLAG_IV_STRIPPED)) { 2082 keyid = ieee80211_get_keyid(rx->skb); 2083 2084 if (unlikely(keyid < 0)) 2085 return RX_DROP_U_NO_KEY_ID; 2086 2087 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]); 2088 } 2089 } 2090 2091 if (!ieee80211_has_protected(fc)) 2092 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb); 2093 2094 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) { 2095 rx->key = ptk_idx ? ptk_idx : sta_ptk; 2096 if ((status->flag & RX_FLAG_DECRYPTED) && 2097 (status->flag & RX_FLAG_IV_STRIPPED)) 2098 return RX_CONTINUE; 2099 /* Skip decryption if the frame is not protected. */ 2100 if (!ieee80211_has_protected(fc)) 2101 return RX_CONTINUE; 2102 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) { 2103 /* Broadcast/multicast robust management frame / BIP */ 2104 if ((status->flag & RX_FLAG_DECRYPTED) && 2105 (status->flag & RX_FLAG_IV_STRIPPED)) 2106 return RX_CONTINUE; 2107 2108 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS || 2109 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS + 2110 NUM_DEFAULT_BEACON_KEYS) { 2111 if (rx->sdata->dev) 2112 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev, 2113 skb->data, 2114 skb->len); 2115 return RX_DROP_U_BAD_BCN_KEYIDX; 2116 } 2117 2118 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx); 2119 if (!rx->key) 2120 return RX_CONTINUE; /* Beacon protection not in use */ 2121 } else if (mmie_keyidx >= 0) { 2122 /* Broadcast/multicast robust management frame / BIP */ 2123 if ((status->flag & RX_FLAG_DECRYPTED) && 2124 (status->flag & RX_FLAG_IV_STRIPPED)) 2125 return RX_CONTINUE; 2126 2127 if (mmie_keyidx < NUM_DEFAULT_KEYS || 2128 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) 2129 return RX_DROP_U_BAD_MGMT_KEYIDX; /* unexpected BIP keyidx */ 2130 if (rx->link_sta) { 2131 if (ieee80211_is_group_privacy_action(skb) && 2132 test_sta_flag(rx->sta, WLAN_STA_MFP)) 2133 return RX_DROP_U_UNPROTECTED; 2134 2135 rx->key = rcu_dereference(rx->link_sta->gtk[mmie_keyidx]); 2136 } 2137 if (!rx->key) 2138 rx->key = rcu_dereference(rx->link->gtk[mmie_keyidx]); 2139 } else if (!ieee80211_has_protected(fc)) { 2140 /* 2141 * The frame was not protected, so skip decryption. However, we 2142 * need to set rx->key if there is a key that could have been 2143 * used so that the frame may be dropped if encryption would 2144 * have been expected. 2145 */ 2146 struct ieee80211_key *key = NULL; 2147 int i; 2148 2149 if (ieee80211_is_beacon(fc)) { 2150 key = ieee80211_rx_get_bigtk(rx, -1); 2151 } else if (ieee80211_is_mgmt(fc) && 2152 is_multicast_ether_addr(hdr->addr1)) { 2153 key = rcu_dereference(rx->link->default_mgmt_key); 2154 } else { 2155 if (rx->link_sta) { 2156 for (i = 0; i < NUM_DEFAULT_KEYS; i++) { 2157 key = rcu_dereference(rx->link_sta->gtk[i]); 2158 if (key) 2159 break; 2160 } 2161 } 2162 if (!key) { 2163 for (i = 0; i < NUM_DEFAULT_KEYS; i++) { 2164 key = rcu_dereference(rx->link->gtk[i]); 2165 if (key) 2166 break; 2167 } 2168 } 2169 } 2170 if (key) 2171 rx->key = key; 2172 return RX_CONTINUE; 2173 } else { 2174 /* 2175 * The device doesn't give us the IV so we won't be 2176 * able to look up the key. That's ok though, we 2177 * don't need to decrypt the frame, we just won't 2178 * be able to keep statistics accurate. 2179 * Except for key threshold notifications, should 2180 * we somehow allow the driver to tell us which key 2181 * the hardware used if this flag is set? 2182 */ 2183 if ((status->flag & RX_FLAG_DECRYPTED) && 2184 (status->flag & RX_FLAG_IV_STRIPPED)) 2185 return RX_CONTINUE; 2186 2187 keyidx = ieee80211_get_keyid(rx->skb); 2188 2189 if (unlikely(keyidx < 0)) 2190 return RX_DROP_U_NO_KEY_ID; 2191 2192 /* check per-station GTK first, if multicast packet */ 2193 if (is_multicast_ether_addr(hdr->addr1) && rx->link_sta) 2194 rx->key = rcu_dereference(rx->link_sta->gtk[keyidx]); 2195 2196 /* if not found, try default key */ 2197 if (!rx->key) { 2198 if (is_multicast_ether_addr(hdr->addr1)) 2199 rx->key = rcu_dereference(rx->link->gtk[keyidx]); 2200 if (!rx->key) 2201 rx->key = rcu_dereference(rx->sdata->keys[keyidx]); 2202 2203 /* 2204 * RSNA-protected unicast frames should always be 2205 * sent with pairwise or station-to-station keys, 2206 * but for WEP we allow using a key index as well. 2207 */ 2208 if (rx->key && 2209 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 && 2210 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 && 2211 !is_multicast_ether_addr(hdr->addr1)) 2212 rx->key = NULL; 2213 } 2214 } 2215 2216 if (rx->key) { 2217 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED)) 2218 return RX_DROP_U_KEY_TAINTED; 2219 2220 /* TODO: add threshold stuff again */ 2221 } else { 2222 return RX_DROP_U_UNPROTECTED; 2223 } 2224 2225 switch (rx->key->conf.cipher) { 2226 case WLAN_CIPHER_SUITE_WEP40: 2227 case WLAN_CIPHER_SUITE_WEP104: 2228 result = ieee80211_crypto_wep_decrypt(rx); 2229 break; 2230 case WLAN_CIPHER_SUITE_TKIP: 2231 result = ieee80211_crypto_tkip_decrypt(rx); 2232 break; 2233 case WLAN_CIPHER_SUITE_CCMP: 2234 result = ieee80211_crypto_ccmp_decrypt( 2235 rx, IEEE80211_CCMP_MIC_LEN); 2236 break; 2237 case WLAN_CIPHER_SUITE_CCMP_256: 2238 result = ieee80211_crypto_ccmp_decrypt( 2239 rx, IEEE80211_CCMP_256_MIC_LEN); 2240 break; 2241 case WLAN_CIPHER_SUITE_AES_CMAC: 2242 result = ieee80211_crypto_aes_cmac_decrypt( 2243 rx, IEEE80211_CMAC_128_MIC_LEN); 2244 break; 2245 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 2246 result = ieee80211_crypto_aes_cmac_decrypt( 2247 rx, IEEE80211_CMAC_256_MIC_LEN); 2248 break; 2249 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 2250 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 2251 result = ieee80211_crypto_aes_gmac_decrypt(rx); 2252 break; 2253 case WLAN_CIPHER_SUITE_GCMP: 2254 case WLAN_CIPHER_SUITE_GCMP_256: 2255 result = ieee80211_crypto_gcmp_decrypt(rx); 2256 break; 2257 default: 2258 result = RX_DROP_U_BAD_CIPHER; 2259 } 2260 2261 /* the hdr variable is invalid after the decrypt handlers */ 2262 2263 /* either the frame has been decrypted or will be dropped */ 2264 status->flag |= RX_FLAG_DECRYPTED; 2265 2266 if (unlikely(ieee80211_is_beacon(fc) && RX_RES_IS_UNUSABLE(result) && 2267 rx->sdata->dev)) 2268 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev, 2269 skb->data, skb->len); 2270 2271 return result; 2272 } 2273 2274 void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache) 2275 { 2276 int i; 2277 2278 for (i = 0; i < ARRAY_SIZE(cache->entries); i++) 2279 skb_queue_head_init(&cache->entries[i].skb_list); 2280 } 2281 2282 void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache) 2283 { 2284 int i; 2285 2286 for (i = 0; i < ARRAY_SIZE(cache->entries); i++) 2287 __skb_queue_purge(&cache->entries[i].skb_list); 2288 } 2289 2290 static inline struct ieee80211_fragment_entry * 2291 ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache, 2292 unsigned int frag, unsigned int seq, int rx_queue, 2293 struct sk_buff **skb) 2294 { 2295 struct ieee80211_fragment_entry *entry; 2296 2297 entry = &cache->entries[cache->next++]; 2298 if (cache->next >= IEEE80211_FRAGMENT_MAX) 2299 cache->next = 0; 2300 2301 __skb_queue_purge(&entry->skb_list); 2302 2303 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */ 2304 *skb = NULL; 2305 entry->first_frag_time = jiffies; 2306 entry->seq = seq; 2307 entry->rx_queue = rx_queue; 2308 entry->last_frag = frag; 2309 entry->check_sequential_pn = false; 2310 entry->extra_len = 0; 2311 2312 return entry; 2313 } 2314 2315 static inline struct ieee80211_fragment_entry * 2316 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache, 2317 unsigned int frag, unsigned int seq, 2318 int rx_queue, struct ieee80211_hdr *hdr) 2319 { 2320 struct ieee80211_fragment_entry *entry; 2321 int i, idx; 2322 2323 idx = cache->next; 2324 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) { 2325 struct ieee80211_hdr *f_hdr; 2326 struct sk_buff *f_skb; 2327 2328 idx--; 2329 if (idx < 0) 2330 idx = IEEE80211_FRAGMENT_MAX - 1; 2331 2332 entry = &cache->entries[idx]; 2333 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq || 2334 entry->rx_queue != rx_queue || 2335 entry->last_frag + 1 != frag) 2336 continue; 2337 2338 f_skb = __skb_peek(&entry->skb_list); 2339 f_hdr = (struct ieee80211_hdr *) f_skb->data; 2340 2341 /* 2342 * Check ftype and addresses are equal, else check next fragment 2343 */ 2344 if (((hdr->frame_control ^ f_hdr->frame_control) & 2345 cpu_to_le16(IEEE80211_FCTL_FTYPE)) || 2346 !ether_addr_equal(hdr->addr1, f_hdr->addr1) || 2347 !ether_addr_equal(hdr->addr2, f_hdr->addr2)) 2348 continue; 2349 2350 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) { 2351 __skb_queue_purge(&entry->skb_list); 2352 continue; 2353 } 2354 return entry; 2355 } 2356 2357 return NULL; 2358 } 2359 2360 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc) 2361 { 2362 return rx->key && 2363 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP || 2364 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 || 2365 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP || 2366 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) && 2367 ieee80211_has_protected(fc); 2368 } 2369 2370 static ieee80211_rx_result debug_noinline 2371 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx) 2372 { 2373 struct ieee80211_fragment_cache *cache = &rx->sdata->frags; 2374 struct ieee80211_hdr *hdr; 2375 u16 sc; 2376 __le16 fc; 2377 unsigned int frag, seq; 2378 struct ieee80211_fragment_entry *entry; 2379 struct sk_buff *skb; 2380 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 2381 2382 hdr = (struct ieee80211_hdr *)rx->skb->data; 2383 fc = hdr->frame_control; 2384 2385 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc)) 2386 return RX_CONTINUE; 2387 2388 sc = le16_to_cpu(hdr->seq_ctrl); 2389 frag = sc & IEEE80211_SCTL_FRAG; 2390 2391 if (rx->sta) 2392 cache = &rx->sta->frags; 2393 2394 if (likely(!ieee80211_has_morefrags(fc) && frag == 0)) 2395 goto out; 2396 2397 if (is_multicast_ether_addr(hdr->addr1)) 2398 return RX_DROP_U_MCAST_FRAGMENT; 2399 2400 I802_DEBUG_INC(rx->local->rx_handlers_fragments); 2401 2402 if (skb_linearize(rx->skb)) 2403 return RX_DROP_U_OOM; 2404 2405 /* 2406 * skb_linearize() might change the skb->data and 2407 * previously cached variables (in this case, hdr) need to 2408 * be refreshed with the new data. 2409 */ 2410 hdr = (struct ieee80211_hdr *)rx->skb->data; 2411 seq = (sc & IEEE80211_SCTL_SEQ) >> 4; 2412 2413 if (frag == 0) { 2414 /* This is the first fragment of a new frame. */ 2415 entry = ieee80211_reassemble_add(cache, frag, seq, 2416 rx->seqno_idx, &(rx->skb)); 2417 if (requires_sequential_pn(rx, fc)) { 2418 int queue = rx->security_idx; 2419 2420 /* Store CCMP/GCMP PN so that we can verify that the 2421 * next fragment has a sequential PN value. 2422 */ 2423 entry->check_sequential_pn = true; 2424 entry->is_protected = true; 2425 entry->key_color = rx->key->color; 2426 memcpy(entry->last_pn, 2427 rx->key->u.ccmp.rx_pn[queue], 2428 IEEE80211_CCMP_PN_LEN); 2429 BUILD_BUG_ON(offsetof(struct ieee80211_key, 2430 u.ccmp.rx_pn) != 2431 offsetof(struct ieee80211_key, 2432 u.gcmp.rx_pn)); 2433 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) != 2434 sizeof(rx->key->u.gcmp.rx_pn[queue])); 2435 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != 2436 IEEE80211_GCMP_PN_LEN); 2437 } else if (rx->key && 2438 (ieee80211_has_protected(fc) || 2439 (status->flag & RX_FLAG_DECRYPTED))) { 2440 entry->is_protected = true; 2441 entry->key_color = rx->key->color; 2442 } 2443 return RX_QUEUED; 2444 } 2445 2446 /* This is a fragment for a frame that should already be pending in 2447 * fragment cache. Add this fragment to the end of the pending entry. 2448 */ 2449 entry = ieee80211_reassemble_find(cache, frag, seq, 2450 rx->seqno_idx, hdr); 2451 if (!entry) { 2452 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag); 2453 return RX_DROP_U_DEFRAG_MISMATCH; 2454 } 2455 2456 /* "The receiver shall discard MSDUs and MMPDUs whose constituent 2457 * MPDU PN values are not incrementing in steps of 1." 2458 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP) 2459 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP) 2460 */ 2461 if (entry->check_sequential_pn) { 2462 int i; 2463 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn; 2464 2465 if (!requires_sequential_pn(rx, fc)) 2466 return RX_DROP_U_NONSEQ_PN; 2467 2468 /* Prevent mixed key and fragment cache attacks */ 2469 if (entry->key_color != rx->key->color) 2470 return RX_DROP_U_BAD_KEY_COLOR; 2471 2472 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN); 2473 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) { 2474 pn[i]++; 2475 if (pn[i]) 2476 break; 2477 } 2478 2479 rpn = rx->ccm_gcm.pn; 2480 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN)) 2481 return RX_DROP_U_REPLAY; 2482 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN); 2483 } else if (entry->is_protected && 2484 (!rx->key || 2485 (!ieee80211_has_protected(fc) && 2486 !(status->flag & RX_FLAG_DECRYPTED)) || 2487 rx->key->color != entry->key_color)) { 2488 /* Drop this as a mixed key or fragment cache attack, even 2489 * if for TKIP Michael MIC should protect us, and WEP is a 2490 * lost cause anyway. 2491 */ 2492 return RX_DROP_U_EXPECT_DEFRAG_PROT; 2493 } else if (entry->is_protected && rx->key && 2494 entry->key_color != rx->key->color && 2495 (status->flag & RX_FLAG_DECRYPTED)) { 2496 return RX_DROP_U_BAD_KEY_COLOR; 2497 } 2498 2499 skb_pull(rx->skb, ieee80211_hdrlen(fc)); 2500 __skb_queue_tail(&entry->skb_list, rx->skb); 2501 entry->last_frag = frag; 2502 entry->extra_len += rx->skb->len; 2503 if (ieee80211_has_morefrags(fc)) { 2504 rx->skb = NULL; 2505 return RX_QUEUED; 2506 } 2507 2508 rx->skb = __skb_dequeue(&entry->skb_list); 2509 if (skb_tailroom(rx->skb) < entry->extra_len) { 2510 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag); 2511 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len, 2512 GFP_ATOMIC))) { 2513 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag); 2514 __skb_queue_purge(&entry->skb_list); 2515 return RX_DROP_U_OOM; 2516 } 2517 } 2518 while ((skb = __skb_dequeue(&entry->skb_list))) { 2519 skb_put_data(rx->skb, skb->data, skb->len); 2520 dev_kfree_skb(skb); 2521 } 2522 2523 out: 2524 ieee80211_led_rx(rx->local); 2525 if (rx->sta) 2526 rx->link_sta->rx_stats.packets++; 2527 return RX_CONTINUE; 2528 } 2529 2530 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx) 2531 { 2532 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED))) 2533 return -EACCES; 2534 2535 return 0; 2536 } 2537 2538 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc) 2539 { 2540 struct sk_buff *skb = rx->skb; 2541 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 2542 2543 /* 2544 * Pass through unencrypted frames if the hardware has 2545 * decrypted them already. 2546 */ 2547 if (status->flag & RX_FLAG_DECRYPTED) 2548 return 0; 2549 2550 /* Drop unencrypted frames if key is set. */ 2551 if (unlikely(!ieee80211_has_protected(fc) && 2552 !ieee80211_is_any_nullfunc(fc) && 2553 ieee80211_is_data(fc) && rx->key)) 2554 return -EACCES; 2555 2556 return 0; 2557 } 2558 2559 VISIBLE_IF_MAC80211_KUNIT ieee80211_rx_result 2560 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx) 2561 { 2562 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 2563 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data; 2564 __le16 fc = mgmt->frame_control; 2565 2566 /* 2567 * Pass through unencrypted frames if the hardware has 2568 * decrypted them already. 2569 */ 2570 if (status->flag & RX_FLAG_DECRYPTED) 2571 return RX_CONTINUE; 2572 2573 /* drop unicast protected dual (that wasn't protected) */ 2574 if (ieee80211_is_action(fc) && 2575 mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION) 2576 return RX_DROP_U_UNPROT_DUAL; 2577 2578 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) { 2579 if (unlikely(!ieee80211_has_protected(fc) && 2580 ieee80211_is_unicast_robust_mgmt_frame(rx->skb))) { 2581 if (ieee80211_is_deauth(fc) || 2582 ieee80211_is_disassoc(fc)) { 2583 /* 2584 * Permit unprotected deauth/disassoc frames 2585 * during 4-way-HS (key is installed after HS). 2586 */ 2587 if (!rx->key) 2588 return RX_CONTINUE; 2589 2590 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev, 2591 rx->skb->data, 2592 rx->skb->len); 2593 } 2594 return RX_DROP_U_UNPROT_UCAST_MGMT; 2595 } 2596 /* BIP does not use Protected field, so need to check MMIE */ 2597 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) && 2598 ieee80211_get_mmie_keyidx(rx->skb) < 0)) { 2599 if (ieee80211_is_deauth(fc) || 2600 ieee80211_is_disassoc(fc)) 2601 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev, 2602 rx->skb->data, 2603 rx->skb->len); 2604 return RX_DROP_U_UNPROT_MCAST_MGMT; 2605 } 2606 if (unlikely(ieee80211_is_beacon(fc) && rx->key && 2607 ieee80211_get_mmie_keyidx(rx->skb) < 0)) { 2608 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev, 2609 rx->skb->data, 2610 rx->skb->len); 2611 return RX_DROP_U_UNPROT_BEACON; 2612 } 2613 /* 2614 * When using MFP, Action frames are not allowed prior to 2615 * having configured keys. 2616 */ 2617 if (unlikely(ieee80211_is_action(fc) && !rx->key && 2618 ieee80211_is_robust_mgmt_frame(rx->skb))) 2619 return RX_DROP_U_UNPROT_ACTION; 2620 2621 /* drop unicast public action frames when using MPF */ 2622 if (is_unicast_ether_addr(mgmt->da) && 2623 ieee80211_is_protected_dual_of_public_action(rx->skb)) 2624 return RX_DROP_U_UNPROT_UNICAST_PUB_ACTION; 2625 } 2626 2627 /* 2628 * Drop robust action frames before assoc regardless of MFP state, 2629 * after assoc we also have decided on MFP or not. 2630 */ 2631 if (ieee80211_is_action(fc) && 2632 ieee80211_is_robust_mgmt_frame(rx->skb) && 2633 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC))) 2634 return RX_DROP_U_UNPROT_ROBUST_ACTION; 2635 2636 /* 2637 * Drop unprotected (Re)Association Request/Response frame received from 2638 * an EPP Peer. 2639 */ 2640 if (!ieee80211_has_protected(fc) && 2641 ieee80211_require_encrypted_assoc(fc, rx->sta)) 2642 return RX_DROP_U_UNPROT_UCAST_MGMT; 2643 2644 return RX_CONTINUE; 2645 } 2646 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_drop_unencrypted_mgmt); 2647 2648 static ieee80211_rx_result 2649 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control) 2650 { 2651 struct ieee80211_sub_if_data *sdata = rx->sdata; 2652 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 2653 bool check_port_control = false; 2654 struct ethhdr *ehdr; 2655 int ret; 2656 2657 *port_control = false; 2658 if (ieee80211_has_a4(hdr->frame_control) && 2659 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta) 2660 return RX_DROP_U_UNEXPECTED_VLAN_4ADDR; 2661 2662 if (sdata->vif.type == NL80211_IFTYPE_STATION && 2663 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) { 2664 if (!sdata->u.mgd.use_4addr) 2665 return RX_DROP_U_UNEXPECTED_STA_4ADDR; 2666 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr)) 2667 check_port_control = true; 2668 } 2669 2670 if (is_multicast_ether_addr(hdr->addr1) && 2671 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta) 2672 return RX_DROP_U_UNEXPECTED_VLAN_MCAST; 2673 2674 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type); 2675 if (ret < 0) 2676 return RX_DROP_U_INVALID_8023; 2677 2678 ehdr = (struct ethhdr *) rx->skb->data; 2679 if (ehdr->h_proto == rx->sdata->control_port_protocol) 2680 *port_control = true; 2681 else if (check_port_control) 2682 return RX_DROP_U_NOT_PORT_CONTROL; 2683 2684 return RX_CONTINUE; 2685 } 2686 2687 bool ieee80211_is_our_addr(struct ieee80211_sub_if_data *sdata, 2688 const u8 *addr, int *out_link_id) 2689 { 2690 unsigned int link_id; 2691 2692 /* non-MLO, or MLD address replaced by hardware */ 2693 if (ether_addr_equal(sdata->vif.addr, addr)) 2694 return true; 2695 2696 if (!ieee80211_vif_is_mld(&sdata->vif)) 2697 return false; 2698 2699 for (link_id = 0; link_id < ARRAY_SIZE(sdata->vif.link_conf); link_id++) { 2700 struct ieee80211_bss_conf *conf; 2701 2702 conf = rcu_dereference(sdata->vif.link_conf[link_id]); 2703 2704 if (!conf) 2705 continue; 2706 if (ether_addr_equal(conf->addr, addr)) { 2707 if (out_link_id) 2708 *out_link_id = link_id; 2709 return true; 2710 } 2711 } 2712 2713 return false; 2714 } 2715 2716 /* 2717 * requires that rx->skb is a frame with ethernet header 2718 */ 2719 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc) 2720 { 2721 static const u8 pae_group_addr[ETH_ALEN] __aligned(2) 2722 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 }; 2723 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data; 2724 2725 /* 2726 * Allow EAPOL frames to us/the PAE group address regardless of 2727 * whether the frame was encrypted or not, and always disallow 2728 * all other destination addresses for them. 2729 */ 2730 if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol)) 2731 return ieee80211_is_our_addr(rx->sdata, ehdr->h_dest, NULL) || 2732 ether_addr_equal(ehdr->h_dest, pae_group_addr); 2733 2734 if (ieee80211_802_1x_port_control(rx) || 2735 ieee80211_drop_unencrypted(rx, fc)) 2736 return false; 2737 2738 return true; 2739 } 2740 2741 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb, 2742 struct ieee80211_rx_data *rx) 2743 { 2744 struct ieee80211_sub_if_data *sdata = rx->sdata; 2745 struct net_device *dev = sdata->dev; 2746 2747 if (unlikely((skb->protocol == sdata->control_port_protocol || 2748 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) && 2749 !sdata->control_port_no_preauth)) && 2750 sdata->control_port_over_nl80211)) { 2751 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 2752 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED); 2753 2754 cfg80211_rx_control_port(dev, skb, noencrypt, rx->link_id); 2755 dev_kfree_skb(skb); 2756 } else { 2757 struct ethhdr *ehdr = (void *)skb_mac_header(skb); 2758 2759 memset(skb->cb, 0, sizeof(skb->cb)); 2760 2761 /* 2762 * 802.1X over 802.11 requires that the authenticator address 2763 * be used for EAPOL frames. However, 802.1X allows the use of 2764 * the PAE group address instead. If the interface is part of 2765 * a bridge and we pass the frame with the PAE group address, 2766 * then the bridge will forward it to the network (even if the 2767 * client was not associated yet), which isn't supposed to 2768 * happen. 2769 * To avoid that, rewrite the destination address to our own 2770 * address, so that the authenticator (e.g. hostapd) will see 2771 * the frame, but bridge won't forward it anywhere else. Note 2772 * that due to earlier filtering, the only other address can 2773 * be the PAE group address, unless the hardware allowed them 2774 * through in 802.3 offloaded mode. 2775 */ 2776 if (unlikely(skb->protocol == sdata->control_port_protocol && 2777 !ether_addr_equal(ehdr->h_dest, sdata->vif.addr))) 2778 ether_addr_copy(ehdr->h_dest, sdata->vif.addr); 2779 2780 /* deliver to local stack */ 2781 if (rx->list) 2782 list_add_tail(&skb->list, rx->list); 2783 else 2784 netif_receive_skb(skb); 2785 } 2786 } 2787 2788 /* 2789 * requires that rx->skb is a frame with ethernet header 2790 */ 2791 static void 2792 ieee80211_deliver_skb(struct ieee80211_rx_data *rx) 2793 { 2794 struct ieee80211_sub_if_data *sdata = rx->sdata; 2795 struct net_device *dev = sdata->dev; 2796 struct sk_buff *skb, *xmit_skb; 2797 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data; 2798 struct sta_info *dsta; 2799 2800 skb = rx->skb; 2801 xmit_skb = NULL; 2802 2803 dev_sw_netstats_rx_add(dev, skb->len); 2804 2805 if (rx->sta) { 2806 /* The seqno index has the same property as needed 2807 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS 2808 * for non-QoS-data frames. Here we know it's a data 2809 * frame, so count MSDUs. 2810 */ 2811 u64_stats_update_begin(&rx->link_sta->rx_stats.syncp); 2812 u64_stats_inc(&rx->link_sta->rx_stats.msdu[rx->seqno_idx]); 2813 u64_stats_update_end(&rx->link_sta->rx_stats.syncp); 2814 } 2815 2816 if ((sdata->vif.type == NL80211_IFTYPE_AP || 2817 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) && 2818 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) && 2819 ehdr->h_proto != rx->sdata->control_port_protocol && 2820 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) { 2821 if (is_multicast_ether_addr(ehdr->h_dest) && 2822 ieee80211_vif_get_num_mcast_if(sdata) != 0) { 2823 /* 2824 * send multicast frames both to higher layers in 2825 * local net stack and back to the wireless medium 2826 */ 2827 xmit_skb = skb_copy(skb, GFP_ATOMIC); 2828 if (!xmit_skb) 2829 net_info_ratelimited("%s: failed to clone multicast frame\n", 2830 dev->name); 2831 } else if (!is_multicast_ether_addr(ehdr->h_dest) && 2832 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) { 2833 dsta = sta_info_get(sdata, ehdr->h_dest); 2834 if (dsta) { 2835 /* 2836 * The destination station is associated to 2837 * this AP (in this VLAN), so send the frame 2838 * directly to it and do not pass it to local 2839 * net stack. 2840 */ 2841 xmit_skb = skb; 2842 skb = NULL; 2843 } 2844 } 2845 } 2846 2847 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 2848 if (skb) { 2849 /* 'align' will only take the values 0 or 2 here since all 2850 * frames are required to be aligned to 2-byte boundaries 2851 * when being passed to mac80211; the code here works just 2852 * as well if that isn't true, but mac80211 assumes it can 2853 * access fields as 2-byte aligned (e.g. for ether_addr_equal) 2854 */ 2855 int align; 2856 2857 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3; 2858 if (align) { 2859 if (WARN_ON(skb_headroom(skb) < 3)) { 2860 dev_kfree_skb(skb); 2861 skb = NULL; 2862 } else { 2863 u8 *data = skb->data; 2864 size_t len = skb_headlen(skb); 2865 skb->data -= align; 2866 memmove(skb->data, data, len); 2867 skb_set_tail_pointer(skb, len); 2868 } 2869 } 2870 } 2871 #endif 2872 2873 if (skb) { 2874 skb->protocol = eth_type_trans(skb, dev); 2875 ieee80211_deliver_skb_to_local_stack(skb, rx); 2876 } 2877 2878 if (xmit_skb) { 2879 /* 2880 * Send to wireless media and increase priority by 256 to 2881 * keep the received priority instead of reclassifying 2882 * the frame (see cfg80211_classify8021d). 2883 */ 2884 xmit_skb->priority += 256; 2885 xmit_skb->protocol = htons(ETH_P_802_3); 2886 skb_reset_network_header(xmit_skb); 2887 skb_reset_mac_header(xmit_skb); 2888 dev_queue_xmit(xmit_skb); 2889 } 2890 } 2891 2892 #ifdef CONFIG_MAC80211_MESH 2893 static bool 2894 ieee80211_rx_mesh_fast_forward(struct ieee80211_sub_if_data *sdata, 2895 struct sk_buff *skb, int hdrlen) 2896 { 2897 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 2898 struct ieee80211_mesh_fast_tx_key key = { 2899 .type = MESH_FAST_TX_TYPE_FORWARDED 2900 }; 2901 struct ieee80211_mesh_fast_tx *entry; 2902 struct ieee80211s_hdr *mesh_hdr; 2903 struct tid_ampdu_tx *tid_tx; 2904 struct sta_info *sta; 2905 struct ethhdr eth; 2906 u8 tid; 2907 2908 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(eth)); 2909 if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) 2910 ether_addr_copy(key.addr, mesh_hdr->eaddr1); 2911 else if (!(mesh_hdr->flags & MESH_FLAGS_AE)) 2912 ether_addr_copy(key.addr, skb->data); 2913 else 2914 return false; 2915 2916 entry = mesh_fast_tx_get(sdata, &key); 2917 if (!entry) 2918 return false; 2919 2920 sta = rcu_dereference(entry->mpath->next_hop); 2921 if (!sta) 2922 return false; 2923 2924 if (skb_linearize(skb)) 2925 return false; 2926 2927 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK; 2928 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]); 2929 if (tid_tx) { 2930 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) 2931 return false; 2932 2933 if (tid_tx->timeout) 2934 tid_tx->last_tx = jiffies; 2935 } 2936 2937 ieee80211_aggr_check(sdata, sta, skb); 2938 2939 if (ieee80211_get_8023_tunnel_proto(skb->data + hdrlen, 2940 &skb->protocol)) 2941 hdrlen += ETH_ALEN; 2942 else 2943 skb->protocol = htons(skb->len - hdrlen); 2944 skb_set_network_header(skb, hdrlen + 2); 2945 2946 skb->dev = sdata->dev; 2947 memcpy(ð, skb->data, ETH_HLEN - 2); 2948 skb_pull(skb, 2); 2949 __ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx, 2950 eth.h_dest, eth.h_source); 2951 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast); 2952 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames); 2953 2954 return true; 2955 } 2956 #endif 2957 2958 static ieee80211_rx_result 2959 ieee80211_rx_mesh_data(struct ieee80211_sub_if_data *sdata, struct sta_info *sta, 2960 struct sk_buff *skb) 2961 { 2962 #ifdef CONFIG_MAC80211_MESH 2963 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 2964 struct ieee80211_local *local = sdata->local; 2965 uint16_t fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA; 2966 struct ieee80211_hdr hdr = { 2967 .frame_control = cpu_to_le16(fc) 2968 }; 2969 struct ieee80211_hdr *fwd_hdr; 2970 struct ieee80211s_hdr *mesh_hdr; 2971 struct ieee80211_tx_info *info; 2972 struct sk_buff *fwd_skb; 2973 struct ethhdr *eth; 2974 bool multicast; 2975 int tailroom = 0; 2976 int hdrlen, mesh_hdrlen; 2977 u8 *qos; 2978 2979 if (!ieee80211_vif_is_mesh(&sdata->vif)) 2980 return RX_CONTINUE; 2981 2982 if (!pskb_may_pull(skb, sizeof(*eth) + 6)) 2983 return RX_DROP_U_RUNT_MESH_DATA; 2984 2985 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(*eth)); 2986 mesh_hdrlen = ieee80211_get_mesh_hdrlen(mesh_hdr); 2987 2988 if (!pskb_may_pull(skb, sizeof(*eth) + mesh_hdrlen)) 2989 return RX_DROP_U_RUNT_MESH_DATA; 2990 2991 eth = (struct ethhdr *)skb->data; 2992 multicast = is_multicast_ether_addr(eth->h_dest); 2993 2994 mesh_hdr = (struct ieee80211s_hdr *)(eth + 1); 2995 if (!mesh_hdr->ttl) 2996 return RX_DROP_U_MESH_NO_TTL; 2997 2998 /* frame is in RMC, don't forward */ 2999 if (is_multicast_ether_addr(eth->h_dest) && 3000 mesh_rmc_check(sdata, eth->h_source, mesh_hdr)) 3001 return RX_DROP_U_MESH_RMC; 3002 3003 /* forward packet */ 3004 if (sdata->crypto_tx_tailroom_needed_cnt) 3005 tailroom = IEEE80211_ENCRYPT_TAILROOM; 3006 3007 if (mesh_hdr->flags & MESH_FLAGS_AE) { 3008 struct mesh_path *mppath; 3009 char *proxied_addr; 3010 bool update = false; 3011 3012 if (multicast) 3013 proxied_addr = mesh_hdr->eaddr1; 3014 else if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) 3015 /* has_a4 already checked in ieee80211_rx_mesh_check */ 3016 proxied_addr = mesh_hdr->eaddr2; 3017 else 3018 return RX_DROP_U_MESH_BAD_AE; 3019 3020 rcu_read_lock(); 3021 mppath = mpp_path_lookup(sdata, proxied_addr); 3022 if (!mppath) { 3023 mpp_path_add(sdata, proxied_addr, eth->h_source); 3024 } else { 3025 spin_lock_bh(&mppath->state_lock); 3026 if (!ether_addr_equal(mppath->mpp, eth->h_source)) { 3027 memcpy(mppath->mpp, eth->h_source, ETH_ALEN); 3028 update = true; 3029 } 3030 mppath->exp_time = jiffies; 3031 spin_unlock_bh(&mppath->state_lock); 3032 } 3033 3034 /* flush fast xmit cache if the address path changed */ 3035 if (update) 3036 mesh_fast_tx_flush_addr(sdata, proxied_addr); 3037 3038 rcu_read_unlock(); 3039 } 3040 3041 /* Frame has reached destination. Don't forward */ 3042 if (ether_addr_equal(sdata->vif.addr, eth->h_dest)) 3043 goto rx_accept; 3044 3045 if (!--mesh_hdr->ttl) { 3046 if (multicast) 3047 goto rx_accept; 3048 3049 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl); 3050 return RX_DROP_U_MESH_TTL_EXPIRED; 3051 } 3052 3053 if (!ifmsh->mshcfg.dot11MeshForwarding) { 3054 if (is_multicast_ether_addr(eth->h_dest)) 3055 goto rx_accept; 3056 3057 return RX_DROP_U_MESH_NOT_FORWARDING; 3058 } 3059 3060 skb_set_queue_mapping(skb, ieee802_1d_to_ac[skb->priority]); 3061 3062 if (!multicast && 3063 ieee80211_rx_mesh_fast_forward(sdata, skb, mesh_hdrlen)) 3064 return RX_QUEUED; 3065 3066 ieee80211_fill_mesh_addresses(&hdr, &hdr.frame_control, 3067 eth->h_dest, eth->h_source); 3068 hdrlen = ieee80211_hdrlen(hdr.frame_control); 3069 if (multicast) { 3070 int extra_head = sizeof(struct ieee80211_hdr) - sizeof(*eth); 3071 3072 fwd_skb = skb_copy_expand(skb, local->tx_headroom + extra_head + 3073 IEEE80211_ENCRYPT_HEADROOM, 3074 tailroom, GFP_ATOMIC); 3075 if (!fwd_skb) 3076 goto rx_accept; 3077 } else { 3078 fwd_skb = skb; 3079 skb = NULL; 3080 3081 if (skb_cow_head(fwd_skb, hdrlen - sizeof(struct ethhdr))) 3082 return RX_DROP_U_OOM; 3083 3084 if (skb_linearize(fwd_skb)) 3085 return RX_DROP_U_OOM; 3086 } 3087 3088 fwd_hdr = skb_push(fwd_skb, hdrlen - sizeof(struct ethhdr)); 3089 memcpy(fwd_hdr, &hdr, hdrlen - 2); 3090 qos = ieee80211_get_qos_ctl(fwd_hdr); 3091 qos[0] = qos[1] = 0; 3092 3093 skb_reset_mac_header(fwd_skb); 3094 hdrlen += mesh_hdrlen; 3095 if (ieee80211_get_8023_tunnel_proto(fwd_skb->data + hdrlen, 3096 &fwd_skb->protocol)) 3097 hdrlen += ETH_ALEN; 3098 else 3099 fwd_skb->protocol = htons(fwd_skb->len - hdrlen); 3100 skb_set_network_header(fwd_skb, hdrlen + 2); 3101 3102 info = IEEE80211_SKB_CB(fwd_skb); 3103 memset(info, 0, sizeof(*info)); 3104 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING; 3105 info->control.vif = &sdata->vif; 3106 info->control.jiffies = jiffies; 3107 fwd_skb->dev = sdata->dev; 3108 if (multicast) { 3109 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast); 3110 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN); 3111 /* update power mode indication when forwarding */ 3112 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr); 3113 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) { 3114 /* mesh power mode flags updated in mesh_nexthop_lookup */ 3115 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast); 3116 } else { 3117 /* unable to resolve next hop */ 3118 if (sta) 3119 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl, 3120 hdr.addr3, 0, 3121 WLAN_REASON_MESH_PATH_NOFORWARD, 3122 sta->sta.addr); 3123 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route); 3124 kfree_skb(fwd_skb); 3125 goto rx_accept; 3126 } 3127 3128 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames); 3129 ieee80211_set_qos_hdr(sdata, fwd_skb); 3130 ieee80211_add_pending_skb(local, fwd_skb); 3131 3132 rx_accept: 3133 if (!skb) 3134 return RX_QUEUED; 3135 3136 ieee80211_strip_8023_mesh_hdr(skb); 3137 #endif 3138 3139 return RX_CONTINUE; 3140 } 3141 3142 static ieee80211_rx_result debug_noinline 3143 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset) 3144 { 3145 struct net_device *dev = rx->sdata->dev; 3146 struct sk_buff *skb = rx->skb; 3147 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 3148 __le16 fc = hdr->frame_control; 3149 struct sk_buff_head frame_list; 3150 struct ethhdr ethhdr; 3151 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source; 3152 3153 if (unlikely(ieee80211_has_a4(hdr->frame_control))) { 3154 check_da = NULL; 3155 check_sa = NULL; 3156 } else switch (rx->sdata->vif.type) { 3157 case NL80211_IFTYPE_AP: 3158 case NL80211_IFTYPE_AP_VLAN: 3159 check_da = NULL; 3160 break; 3161 case NL80211_IFTYPE_STATION: 3162 if (!test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER)) 3163 check_sa = NULL; 3164 break; 3165 case NL80211_IFTYPE_MESH_POINT: 3166 check_sa = NULL; 3167 check_da = NULL; 3168 break; 3169 default: 3170 break; 3171 } 3172 3173 skb->dev = dev; 3174 __skb_queue_head_init(&frame_list); 3175 3176 if (ieee80211_data_to_8023_exthdr(skb, ðhdr, 3177 rx->sdata->vif.addr, 3178 rx->sdata->vif.type, 3179 data_offset, true)) 3180 return RX_DROP_U_BAD_AMSDU; 3181 3182 if (rx->sta->amsdu_mesh_control < 0) { 3183 s8 valid = -1; 3184 int i; 3185 3186 for (i = 0; i <= 2; i++) { 3187 if (!ieee80211_is_valid_amsdu(skb, i)) 3188 continue; 3189 3190 if (valid >= 0) { 3191 /* ambiguous */ 3192 valid = -1; 3193 break; 3194 } 3195 3196 valid = i; 3197 } 3198 3199 rx->sta->amsdu_mesh_control = valid; 3200 } 3201 3202 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr, 3203 rx->sdata->vif.type, 3204 rx->local->hw.extra_tx_headroom, 3205 check_da, check_sa, 3206 rx->sta->amsdu_mesh_control); 3207 3208 while (!skb_queue_empty(&frame_list)) { 3209 rx->skb = __skb_dequeue(&frame_list); 3210 3211 switch (ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb)) { 3212 case RX_QUEUED: 3213 break; 3214 case RX_CONTINUE: 3215 if (ieee80211_frame_allowed(rx, fc)) { 3216 ieee80211_deliver_skb(rx); 3217 break; 3218 } 3219 fallthrough; 3220 default: 3221 dev_kfree_skb(rx->skb); 3222 } 3223 } 3224 3225 return RX_QUEUED; 3226 } 3227 3228 static ieee80211_rx_result debug_noinline 3229 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx) 3230 { 3231 struct sk_buff *skb = rx->skb; 3232 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 3233 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 3234 __le16 fc = hdr->frame_control; 3235 3236 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) 3237 return RX_CONTINUE; 3238 3239 if (unlikely(!ieee80211_is_data(fc))) 3240 return RX_CONTINUE; 3241 3242 if (unlikely(!ieee80211_is_data_present(fc))) 3243 return RX_DROP_U_AMSDU_WITHOUT_DATA; 3244 3245 if (unlikely(ieee80211_has_a4(hdr->frame_control))) { 3246 switch (rx->sdata->vif.type) { 3247 case NL80211_IFTYPE_AP_VLAN: 3248 if (!rx->sdata->u.vlan.sta) 3249 return RX_DROP_U_BAD_4ADDR; 3250 break; 3251 case NL80211_IFTYPE_STATION: 3252 if (!rx->sdata->u.mgd.use_4addr) 3253 return RX_DROP_U_BAD_4ADDR; 3254 break; 3255 case NL80211_IFTYPE_MESH_POINT: 3256 break; 3257 default: 3258 return RX_DROP_U_BAD_4ADDR; 3259 } 3260 } 3261 3262 if (is_multicast_ether_addr(hdr->addr1) || !rx->sta) 3263 return RX_DROP_U_BAD_AMSDU; 3264 3265 if (rx->key) { 3266 /* 3267 * We should not receive A-MSDUs on pre-HT connections, 3268 * and HT connections cannot use old ciphers. Thus drop 3269 * them, as in those cases we couldn't even have SPP 3270 * A-MSDUs or such. 3271 */ 3272 switch (rx->key->conf.cipher) { 3273 case WLAN_CIPHER_SUITE_WEP40: 3274 case WLAN_CIPHER_SUITE_WEP104: 3275 case WLAN_CIPHER_SUITE_TKIP: 3276 return RX_DROP_U_BAD_AMSDU_CIPHER; 3277 default: 3278 break; 3279 } 3280 } 3281 3282 return __ieee80211_rx_h_amsdu(rx, 0); 3283 } 3284 3285 static ieee80211_rx_result debug_noinline 3286 ieee80211_rx_h_data(struct ieee80211_rx_data *rx) 3287 { 3288 struct ieee80211_sub_if_data *sdata = rx->sdata; 3289 struct ieee80211_local *local = rx->local; 3290 struct net_device *dev = sdata->dev; 3291 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 3292 __le16 fc = hdr->frame_control; 3293 ieee80211_rx_result res; 3294 bool port_control; 3295 3296 if (unlikely(!ieee80211_is_data(hdr->frame_control))) 3297 return RX_CONTINUE; 3298 3299 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) 3300 return RX_DROP_U_NULL_DATA; 3301 3302 /* Send unexpected-4addr-frame event to hostapd */ 3303 if (ieee80211_has_a4(hdr->frame_control) && 3304 sdata->vif.type == NL80211_IFTYPE_AP) { 3305 if (rx->sta && 3306 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT)) 3307 cfg80211_rx_unexpected_4addr_frame( 3308 rx->sdata->dev, rx->sta->sta.addr, rx->link_id, 3309 GFP_ATOMIC); 3310 return RX_DROP_U_UNEXPECTED_4ADDR; 3311 } 3312 3313 res = __ieee80211_data_to_8023(rx, &port_control); 3314 if (unlikely(res != RX_CONTINUE)) 3315 return res; 3316 3317 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb); 3318 if (res != RX_CONTINUE) 3319 return res; 3320 3321 if (!ieee80211_frame_allowed(rx, fc)) 3322 return RX_DROP_U_PORT_CONTROL; 3323 3324 /* directly handle TDLS channel switch requests/responses */ 3325 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto == 3326 cpu_to_be16(ETH_P_TDLS))) { 3327 struct ieee80211_tdls_data *tf = (void *)rx->skb->data; 3328 3329 if (pskb_may_pull(rx->skb, 3330 offsetof(struct ieee80211_tdls_data, u)) && 3331 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE && 3332 tf->category == WLAN_CATEGORY_TDLS && 3333 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST || 3334 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) { 3335 rx->skb->protocol = cpu_to_be16(ETH_P_TDLS); 3336 __ieee80211_queue_skb_to_iface(sdata, rx->link_id, 3337 rx->sta, rx->skb); 3338 return RX_QUEUED; 3339 } 3340 } 3341 3342 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 3343 unlikely(port_control) && sdata->bss) { 3344 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, 3345 u.ap); 3346 dev = sdata->dev; 3347 rx->sdata = sdata; 3348 } 3349 3350 rx->skb->dev = dev; 3351 3352 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) && 3353 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 && 3354 !is_multicast_ether_addr( 3355 ((struct ethhdr *)rx->skb->data)->h_dest) && 3356 (!local->scanning && 3357 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) 3358 mod_timer(&local->dynamic_ps_timer, jiffies + 3359 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout)); 3360 3361 ieee80211_deliver_skb(rx); 3362 3363 return RX_QUEUED; 3364 } 3365 3366 static ieee80211_rx_result debug_noinline 3367 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames) 3368 { 3369 struct sk_buff *skb = rx->skb; 3370 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data; 3371 struct tid_ampdu_rx *tid_agg_rx; 3372 u16 start_seq_num; 3373 u16 tid; 3374 3375 if (likely(!ieee80211_is_ctl(bar->frame_control))) 3376 return RX_CONTINUE; 3377 3378 if (ieee80211_is_back_req(bar->frame_control)) { 3379 struct { 3380 __le16 control, start_seq_num; 3381 } __packed bar_data; 3382 struct ieee80211_event event = { 3383 .type = BAR_RX_EVENT, 3384 }; 3385 3386 if (!rx->sta) 3387 return RX_DROP_U_UNKNOWN_STA; 3388 3389 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control), 3390 &bar_data, sizeof(bar_data))) 3391 return RX_DROP_U_RUNT_BAR; 3392 3393 tid = le16_to_cpu(bar_data.control) >> 12; 3394 3395 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) && 3396 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg)) 3397 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid, 3398 WLAN_BACK_RECIPIENT, 3399 WLAN_REASON_QSTA_REQUIRE_SETUP, 3400 ieee80211_s1g_use_ndp_ba(rx->sdata, 3401 rx->sta)); 3402 3403 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]); 3404 if (!tid_agg_rx) 3405 return RX_DROP_U_BAR_OUTSIDE_SESSION; 3406 3407 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4; 3408 event.u.ba.tid = tid; 3409 event.u.ba.ssn = start_seq_num; 3410 event.u.ba.sta = &rx->sta->sta; 3411 3412 /* reset session timer */ 3413 if (tid_agg_rx->timeout) 3414 mod_timer(&tid_agg_rx->session_timer, 3415 TU_TO_EXP_TIME(tid_agg_rx->timeout)); 3416 3417 spin_lock(&tid_agg_rx->reorder_lock); 3418 /* release stored frames up to start of BAR */ 3419 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx, 3420 start_seq_num, frames); 3421 spin_unlock(&tid_agg_rx->reorder_lock); 3422 3423 drv_event_callback(rx->local, rx->sdata, &event); 3424 3425 kfree_skb(skb); 3426 return RX_QUEUED; 3427 } 3428 3429 return RX_DROP_U_CTRL_FRAME; 3430 } 3431 3432 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata, 3433 struct ieee80211_mgmt *mgmt, 3434 size_t len) 3435 { 3436 struct ieee80211_local *local = sdata->local; 3437 struct sk_buff *skb; 3438 struct ieee80211_mgmt *resp; 3439 3440 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) { 3441 /* Not to own unicast address */ 3442 return; 3443 } 3444 3445 if (!ether_addr_equal(mgmt->sa, sdata->vif.cfg.ap_addr) || 3446 !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) { 3447 /* Not from the current AP or not associated yet. */ 3448 return; 3449 } 3450 3451 if (len < IEEE80211_MIN_ACTION_SIZE(sa_query)) { 3452 /* Too short SA Query request frame */ 3453 return; 3454 } 3455 3456 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom); 3457 if (skb == NULL) 3458 return; 3459 3460 skb_reserve(skb, local->hw.extra_tx_headroom); 3461 resp = skb_put_zero(skb, IEEE80211_MIN_ACTION_SIZE(sa_query)); 3462 memcpy(resp->da, sdata->vif.cfg.ap_addr, ETH_ALEN); 3463 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN); 3464 memcpy(resp->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN); 3465 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 3466 IEEE80211_STYPE_ACTION); 3467 resp->u.action.category = WLAN_CATEGORY_SA_QUERY; 3468 resp->u.action.action_code = WLAN_ACTION_SA_QUERY_RESPONSE; 3469 memcpy(resp->u.action.sa_query.trans_id, 3470 mgmt->u.action.sa_query.trans_id, 3471 WLAN_SA_QUERY_TR_ID_LEN); 3472 3473 ieee80211_tx_skb(sdata, skb); 3474 } 3475 3476 static void 3477 ieee80211_rx_check_bss_color_collision(struct ieee80211_rx_data *rx) 3478 { 3479 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data; 3480 struct ieee80211_bss_conf *bss_conf; 3481 const struct element *ie; 3482 size_t baselen; 3483 3484 if (!wiphy_ext_feature_isset(rx->local->hw.wiphy, 3485 NL80211_EXT_FEATURE_BSS_COLOR)) 3486 return; 3487 3488 if (ieee80211_hw_check(&rx->local->hw, DETECTS_COLOR_COLLISION)) 3489 return; 3490 3491 bss_conf = rx->link->conf; 3492 if (bss_conf->csa_active || bss_conf->color_change_active || 3493 !bss_conf->he_bss_color.enabled) 3494 return; 3495 3496 baselen = mgmt->u.beacon.variable - rx->skb->data; 3497 if (baselen > rx->skb->len) 3498 return; 3499 3500 ie = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, 3501 mgmt->u.beacon.variable, 3502 rx->skb->len - baselen); 3503 if (ie && ie->datalen >= sizeof(struct ieee80211_he_operation) && 3504 ie->datalen >= ieee80211_he_oper_size(ie->data + 1)) { 3505 const struct ieee80211_he_operation *he_oper; 3506 u8 color; 3507 3508 he_oper = (void *)(ie->data + 1); 3509 if (le32_get_bits(he_oper->he_oper_params, 3510 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED)) 3511 return; 3512 3513 color = le32_get_bits(he_oper->he_oper_params, 3514 IEEE80211_HE_OPERATION_BSS_COLOR_MASK); 3515 if (color == bss_conf->he_bss_color.color) 3516 ieee80211_obss_color_collision_notify(&rx->sdata->vif, 3517 BIT_ULL(color), 3518 bss_conf->link_id); 3519 } 3520 } 3521 3522 static ieee80211_rx_result debug_noinline 3523 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx) 3524 { 3525 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data; 3526 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 3527 3528 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) 3529 return RX_CONTINUE; 3530 3531 /* 3532 * From here on, look only at management frames. 3533 * Data and control frames are already handled, 3534 * and unknown (reserved) frames are useless. 3535 */ 3536 if (rx->skb->len < 24) 3537 return RX_DROP_U_RUNT_MGMT; 3538 3539 if (!ieee80211_is_mgmt(mgmt->frame_control)) 3540 return RX_DROP_U_EXPECTED_MGMT; 3541 3542 /* drop too small action frames */ 3543 if (ieee80211_is_action(mgmt->frame_control) && 3544 rx->skb->len < IEEE80211_MIN_ACTION_SIZE(category)) 3545 return RX_DROP_U_RUNT_ACTION; 3546 3547 /* Drop non-broadcast Beacon frames */ 3548 if (ieee80211_is_beacon(mgmt->frame_control) && 3549 !is_broadcast_ether_addr(mgmt->da)) 3550 return RX_DROP_U_NONBCAST_BEACON; 3551 3552 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && 3553 ieee80211_is_beacon(mgmt->frame_control) && 3554 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) { 3555 int sig = 0; 3556 3557 /* sw bss color collision detection */ 3558 ieee80211_rx_check_bss_color_collision(rx); 3559 3560 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) && 3561 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) 3562 sig = status->signal; 3563 3564 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy, 3565 rx->skb->data, rx->skb->len, 3566 ieee80211_rx_status_to_khz(status), 3567 sig); 3568 rx->flags |= IEEE80211_RX_BEACON_REPORTED; 3569 } 3570 3571 return ieee80211_drop_unencrypted_mgmt(rx); 3572 } 3573 3574 static bool 3575 ieee80211_process_rx_twt_action(struct ieee80211_rx_data *rx) 3576 { 3577 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)rx->skb->data; 3578 struct ieee80211_sub_if_data *sdata = rx->sdata; 3579 3580 /* TWT actions are only supported in AP for the moment */ 3581 if (sdata->vif.type != NL80211_IFTYPE_AP) 3582 return false; 3583 3584 if (!rx->local->ops->add_twt_setup) 3585 return false; 3586 3587 if (!sdata->vif.bss_conf.twt_responder) 3588 return false; 3589 3590 if (!rx->sta) 3591 return false; 3592 3593 switch (mgmt->u.action.action_code) { 3594 case WLAN_S1G_TWT_SETUP: { 3595 struct ieee80211_twt_setup *twt; 3596 3597 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) + 3598 sizeof(struct ieee80211_twt_setup) + 3599 2 /* TWT req_type agrt */) 3600 break; 3601 3602 twt = (void *)mgmt->u.action.s1g.variable; 3603 if (twt->element_id != WLAN_EID_S1G_TWT) 3604 break; 3605 3606 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) + 3607 3 + /* token + tlv */ 3608 twt->length) 3609 break; 3610 3611 return true; /* queue the frame */ 3612 } 3613 case WLAN_S1G_TWT_TEARDOWN: 3614 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) + 1) 3615 break; 3616 3617 return true; /* queue the frame */ 3618 default: 3619 break; 3620 } 3621 3622 return false; 3623 } 3624 3625 static ieee80211_rx_result debug_noinline 3626 ieee80211_rx_h_action(struct ieee80211_rx_data *rx) 3627 { 3628 struct ieee80211_local *local = rx->local; 3629 struct ieee80211_sub_if_data *sdata = rx->sdata; 3630 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data; 3631 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 3632 int len = rx->skb->len; 3633 3634 if (!ieee80211_is_action(mgmt->frame_control)) 3635 return RX_CONTINUE; 3636 3637 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC && 3638 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED && 3639 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT) 3640 return RX_DROP_U_ACTION_UNKNOWN_SRC; 3641 3642 switch (mgmt->u.action.category) { 3643 case WLAN_CATEGORY_HT: 3644 /* reject HT action frames from stations not supporting HT 3645 * or not HE Capable 3646 */ 3647 if (!rx->link_sta->pub->ht_cap.ht_supported && 3648 !rx->link_sta->pub->he_cap.has_he) 3649 goto invalid; 3650 3651 if (sdata->vif.type != NL80211_IFTYPE_STATION && 3652 sdata->vif.type != NL80211_IFTYPE_MESH_POINT && 3653 sdata->vif.type != NL80211_IFTYPE_AP_VLAN && 3654 sdata->vif.type != NL80211_IFTYPE_AP && 3655 sdata->vif.type != NL80211_IFTYPE_ADHOC) 3656 break; 3657 3658 /* verify action & smps_control/chanwidth are present */ 3659 if (len < IEEE80211_MIN_ACTION_SIZE(ht_smps)) 3660 goto invalid; 3661 3662 switch (mgmt->u.action.action_code) { 3663 case WLAN_HT_ACTION_SMPS: { 3664 struct ieee80211_supported_band *sband; 3665 enum ieee80211_smps_mode smps_mode; 3666 struct sta_opmode_info sta_opmode = {}; 3667 3668 if (sdata->vif.type != NL80211_IFTYPE_AP && 3669 sdata->vif.type != NL80211_IFTYPE_AP_VLAN) 3670 goto handled; 3671 3672 /* convert to HT capability */ 3673 switch (mgmt->u.action.ht_smps.smps_control) { 3674 case WLAN_HT_SMPS_CONTROL_DISABLED: 3675 smps_mode = IEEE80211_SMPS_OFF; 3676 break; 3677 case WLAN_HT_SMPS_CONTROL_STATIC: 3678 smps_mode = IEEE80211_SMPS_STATIC; 3679 break; 3680 case WLAN_HT_SMPS_CONTROL_DYNAMIC: 3681 smps_mode = IEEE80211_SMPS_DYNAMIC; 3682 break; 3683 default: 3684 goto invalid; 3685 } 3686 3687 /* if no change do nothing */ 3688 if (rx->link_sta->pub->smps_mode == smps_mode) 3689 goto handled; 3690 rx->link_sta->pub->smps_mode = smps_mode; 3691 sta_opmode.smps_mode = 3692 ieee80211_smps_mode_to_smps_mode(smps_mode); 3693 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED; 3694 3695 sband = rx->local->hw.wiphy->bands[status->band]; 3696 3697 rate_control_rate_update(local, sband, rx->link_sta, 3698 IEEE80211_RC_SMPS_CHANGED); 3699 cfg80211_sta_opmode_change_notify(sdata->dev, 3700 rx->sta->addr, 3701 &sta_opmode, 3702 GFP_ATOMIC); 3703 goto handled; 3704 } 3705 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: { 3706 u8 chanwidth = mgmt->u.action.ht_notify_cw.chanwidth; 3707 3708 if (chanwidth != IEEE80211_HT_CHANWIDTH_20MHZ && 3709 chanwidth != IEEE80211_HT_CHANWIDTH_ANY) 3710 goto invalid; 3711 3712 /* If it doesn't support 40 MHz it can't change ... */ 3713 if (!(rx->link_sta->pub->ht_cap.cap & 3714 IEEE80211_HT_CAP_SUP_WIDTH_20_40)) 3715 goto handled; 3716 3717 goto queue; 3718 } 3719 default: 3720 goto invalid; 3721 } 3722 3723 break; 3724 case WLAN_CATEGORY_PUBLIC: 3725 case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION: 3726 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3727 goto invalid; 3728 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3729 break; 3730 if (!rx->sta) 3731 break; 3732 if (!ether_addr_equal(mgmt->bssid, sdata->deflink.u.mgd.bssid)) 3733 break; 3734 if (mgmt->u.action.action_code != 3735 WLAN_PUB_ACTION_EXT_CHANSW_ANN) 3736 break; 3737 if (len < IEEE80211_MIN_ACTION_SIZE(ext_chan_switch)) 3738 goto invalid; 3739 goto queue; 3740 case WLAN_CATEGORY_VHT: 3741 if (sdata->vif.type != NL80211_IFTYPE_STATION && 3742 sdata->vif.type != NL80211_IFTYPE_MESH_POINT && 3743 sdata->vif.type != NL80211_IFTYPE_AP_VLAN && 3744 sdata->vif.type != NL80211_IFTYPE_AP && 3745 sdata->vif.type != NL80211_IFTYPE_ADHOC) 3746 break; 3747 3748 /* verify action code is present */ 3749 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3750 goto invalid; 3751 3752 switch (mgmt->u.action.action_code) { 3753 case WLAN_VHT_ACTION_OPMODE_NOTIF: { 3754 /* verify opmode is present */ 3755 if (len < IEEE80211_MIN_ACTION_SIZE(vht_opmode_notif)) 3756 goto invalid; 3757 goto queue; 3758 } 3759 case WLAN_VHT_ACTION_GROUPID_MGMT: { 3760 if (len < IEEE80211_MIN_ACTION_SIZE(vht_group_notif)) 3761 goto invalid; 3762 goto queue; 3763 } 3764 default: 3765 break; 3766 } 3767 break; 3768 case WLAN_CATEGORY_BACK: 3769 if (sdata->vif.type != NL80211_IFTYPE_STATION && 3770 sdata->vif.type != NL80211_IFTYPE_MESH_POINT && 3771 sdata->vif.type != NL80211_IFTYPE_AP_VLAN && 3772 sdata->vif.type != NL80211_IFTYPE_AP && 3773 sdata->vif.type != NL80211_IFTYPE_ADHOC && 3774 sdata->vif.type != NL80211_IFTYPE_NAN_DATA) 3775 break; 3776 3777 /* verify action_code is present */ 3778 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3779 break; 3780 3781 switch (mgmt->u.action.action_code) { 3782 case WLAN_ACTION_ADDBA_REQ: 3783 case WLAN_ACTION_NDP_ADDBA_REQ: 3784 if (len < IEEE80211_MIN_ACTION_SIZE(addba_req)) 3785 goto invalid; 3786 break; 3787 case WLAN_ACTION_ADDBA_RESP: 3788 case WLAN_ACTION_NDP_ADDBA_RESP: 3789 if (len < IEEE80211_MIN_ACTION_SIZE(addba_resp)) 3790 goto invalid; 3791 break; 3792 case WLAN_ACTION_DELBA: 3793 case WLAN_ACTION_NDP_DELBA: 3794 if (len < IEEE80211_MIN_ACTION_SIZE(delba)) 3795 goto invalid; 3796 break; 3797 default: 3798 goto invalid; 3799 } 3800 3801 goto queue; 3802 case WLAN_CATEGORY_SPECTRUM_MGMT: 3803 /* verify action_code is present */ 3804 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3805 break; 3806 3807 switch (mgmt->u.action.action_code) { 3808 case WLAN_ACTION_SPCT_MSR_REQ: 3809 if (status->band != NL80211_BAND_5GHZ) 3810 break; 3811 3812 if (len < IEEE80211_MIN_ACTION_SIZE(measurement)) 3813 break; 3814 3815 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3816 break; 3817 3818 ieee80211_process_measurement_req(sdata, mgmt, len); 3819 goto handled; 3820 case WLAN_ACTION_SPCT_CHL_SWITCH: { 3821 u8 *bssid; 3822 if (len < IEEE80211_MIN_ACTION_SIZE(chan_switch)) 3823 break; 3824 3825 if (sdata->vif.type != NL80211_IFTYPE_STATION && 3826 sdata->vif.type != NL80211_IFTYPE_ADHOC && 3827 sdata->vif.type != NL80211_IFTYPE_MESH_POINT) 3828 break; 3829 3830 if (sdata->vif.type == NL80211_IFTYPE_STATION) 3831 bssid = sdata->deflink.u.mgd.bssid; 3832 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) 3833 bssid = sdata->u.ibss.bssid; 3834 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) 3835 bssid = mgmt->sa; 3836 else 3837 break; 3838 3839 if (!ether_addr_equal(mgmt->bssid, bssid)) 3840 break; 3841 3842 goto queue; 3843 } 3844 } 3845 break; 3846 case WLAN_CATEGORY_SELF_PROTECTED: 3847 if (len < IEEE80211_MIN_ACTION_SIZE(self_prot)) 3848 break; 3849 3850 switch (mgmt->u.action.action_code) { 3851 case WLAN_SP_MESH_PEERING_OPEN: 3852 case WLAN_SP_MESH_PEERING_CLOSE: 3853 case WLAN_SP_MESH_PEERING_CONFIRM: 3854 if (!ieee80211_vif_is_mesh(&sdata->vif)) 3855 goto invalid; 3856 if (sdata->u.mesh.user_mpm) 3857 /* userspace handles this frame */ 3858 break; 3859 goto queue; 3860 case WLAN_SP_MGK_INFORM: 3861 case WLAN_SP_MGK_ACK: 3862 if (!ieee80211_vif_is_mesh(&sdata->vif)) 3863 goto invalid; 3864 break; 3865 } 3866 break; 3867 case WLAN_CATEGORY_MESH_ACTION: 3868 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3869 break; 3870 3871 if (!ieee80211_vif_is_mesh(&sdata->vif)) 3872 break; 3873 if (mesh_action_is_path_sel(mgmt) && 3874 !mesh_path_sel_is_hwmp(sdata)) 3875 break; 3876 goto queue; 3877 case WLAN_CATEGORY_S1G: 3878 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3879 break; 3880 3881 switch (mgmt->u.action.action_code) { 3882 case WLAN_S1G_TWT_SETUP: 3883 case WLAN_S1G_TWT_TEARDOWN: 3884 if (ieee80211_process_rx_twt_action(rx)) 3885 goto queue; 3886 break; 3887 default: 3888 break; 3889 } 3890 break; 3891 case WLAN_CATEGORY_PROTECTED_EHT: 3892 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3893 break; 3894 3895 switch (mgmt->u.action.action_code) { 3896 case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ: 3897 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3898 break; 3899 3900 if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_req)) 3901 goto invalid; 3902 goto queue; 3903 case WLAN_PROTECTED_EHT_ACTION_TTLM_RES: 3904 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3905 break; 3906 3907 if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_res)) 3908 goto invalid; 3909 goto queue; 3910 case WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN: 3911 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3912 break; 3913 3914 if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_tear_down)) 3915 goto invalid; 3916 goto queue; 3917 case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP: 3918 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3919 break; 3920 3921 /* The reconfiguration response action frame must 3922 * least one 'Status Duple' entry (3 octets) 3923 */ 3924 if (len < IEEE80211_MIN_ACTION_SIZE(ml_reconf_resp) + 3) 3925 goto invalid; 3926 goto queue; 3927 case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP: 3928 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3929 break; 3930 3931 if (len < IEEE80211_MIN_ACTION_SIZE(epcs) + 3932 IEEE80211_EPCS_ENA_RESP_BODY_LEN) 3933 goto invalid; 3934 goto queue; 3935 case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN: 3936 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3937 break; 3938 3939 if (len < IEEE80211_MIN_ACTION_SIZE(epcs)) 3940 goto invalid; 3941 goto queue; 3942 case WLAN_PROTECTED_EHT_ACTION_EML_OP_MODE_NOTIF: 3943 if (sdata->vif.type != NL80211_IFTYPE_AP) 3944 break; 3945 3946 if (len < IEEE80211_MIN_ACTION_SIZE(eml_omn)) 3947 goto invalid; 3948 goto queue; 3949 default: 3950 break; 3951 } 3952 break; 3953 case WLAN_CATEGORY_PROTECTED_UHR: 3954 if (len < IEEE80211_MIN_ACTION_SIZE(action_code)) 3955 break; 3956 3957 switch (mgmt->u.action.action_code) { 3958 case IEEE80211_PROTECTED_UHR_ACTION_LINK_RECONFIG_REQUEST: 3959 if (sdata->vif.type != NL80211_IFTYPE_AP) 3960 break; 3961 if (len < IEEE80211_MIN_ACTION_SIZE(uhr_link_reconf_req)) 3962 goto invalid; 3963 if (mgmt->u.action.uhr_link_reconf_req.type != 3964 IEEE80211_UHR_LINK_RECONFIG_REQUEST_OMP_REQUEST) 3965 break; 3966 goto queue; 3967 case IEEE80211_PROTECTED_UHR_ACTION_LINK_RECONFIG_NOTIFY: 3968 if (sdata->vif.type != NL80211_IFTYPE_STATION) 3969 break; 3970 3971 if (len < IEEE80211_MIN_ACTION_SIZE(uhr_link_reconf_notif)) 3972 goto invalid; 3973 goto queue; 3974 } 3975 break; 3976 } 3977 3978 return RX_CONTINUE; 3979 3980 invalid: 3981 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM; 3982 /* will return in the next handlers */ 3983 return RX_CONTINUE; 3984 3985 handled: 3986 if (rx->sta) 3987 rx->link_sta->rx_stats.packets++; 3988 dev_kfree_skb(rx->skb); 3989 return RX_QUEUED; 3990 3991 queue: 3992 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb); 3993 return RX_QUEUED; 3994 } 3995 3996 static ieee80211_rx_result debug_noinline 3997 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx) 3998 { 3999 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 4000 struct cfg80211_rx_info info = { 4001 .freq = ieee80211_rx_status_to_khz(status), 4002 .buf = rx->skb->data, 4003 .len = rx->skb->len, 4004 .link_id = rx->link_id, 4005 .have_link_id = rx->link_id >= 0, 4006 }; 4007 4008 /* skip known-bad action frames and return them in the next handler */ 4009 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) 4010 return RX_CONTINUE; 4011 4012 /* 4013 * Getting here means the kernel doesn't know how to handle 4014 * it, but maybe userspace does ... include returned frames 4015 * so userspace can register for those to know whether ones 4016 * it transmitted were processed or returned. 4017 */ 4018 4019 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) && 4020 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) 4021 info.sig_dbm = status->signal; 4022 4023 if (ieee80211_is_timing_measurement(rx->skb) || 4024 ieee80211_is_ftm(rx->skb)) { 4025 info.rx_tstamp = ktime_to_ns(skb_hwtstamps(rx->skb)->hwtstamp); 4026 info.ack_tstamp = ktime_to_ns(status->ack_tx_hwtstamp); 4027 } 4028 4029 if (cfg80211_rx_mgmt_ext(&rx->sdata->wdev, &info)) { 4030 if (rx->sta) 4031 rx->link_sta->rx_stats.packets++; 4032 dev_kfree_skb(rx->skb); 4033 return RX_QUEUED; 4034 } 4035 4036 return RX_CONTINUE; 4037 } 4038 4039 static ieee80211_rx_result debug_noinline 4040 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx) 4041 { 4042 struct ieee80211_sub_if_data *sdata = rx->sdata; 4043 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data; 4044 int len = rx->skb->len; 4045 4046 if (!ieee80211_is_action(mgmt->frame_control)) 4047 return RX_CONTINUE; 4048 4049 switch (mgmt->u.action.category) { 4050 case WLAN_CATEGORY_SA_QUERY: 4051 if (len < IEEE80211_MIN_ACTION_SIZE(sa_query)) 4052 break; 4053 4054 switch (mgmt->u.action.action_code) { 4055 case WLAN_ACTION_SA_QUERY_REQUEST: 4056 if (sdata->vif.type != NL80211_IFTYPE_STATION) 4057 break; 4058 ieee80211_process_sa_query_req(sdata, mgmt, len); 4059 goto handled; 4060 } 4061 break; 4062 } 4063 4064 return RX_CONTINUE; 4065 4066 handled: 4067 if (rx->sta) 4068 rx->link_sta->rx_stats.packets++; 4069 dev_kfree_skb(rx->skb); 4070 return RX_QUEUED; 4071 } 4072 4073 static ieee80211_rx_result debug_noinline 4074 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx) 4075 { 4076 struct ieee80211_local *local = rx->local; 4077 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data; 4078 struct sk_buff *nskb; 4079 struct ieee80211_sub_if_data *sdata = rx->sdata; 4080 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 4081 4082 if (!ieee80211_is_action(mgmt->frame_control)) 4083 return RX_CONTINUE; 4084 4085 /* 4086 * For AP mode, hostapd is responsible for handling any action 4087 * frames that we didn't handle, including returning unknown 4088 * ones. For all other modes we will return them to the sender, 4089 * setting the 0x80 bit in the action category, as required by 4090 * 802.11-2012 9.24.4. 4091 * Newer versions of hostapd use the management frame registration 4092 * mechanisms and old cooked monitor interface is no longer supported. 4093 */ 4094 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) && 4095 (sdata->vif.type == NL80211_IFTYPE_AP || 4096 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) 4097 return RX_DROP_U_MALFORMED_ACTION; 4098 4099 if (is_multicast_ether_addr(mgmt->da)) 4100 return RX_DROP_U_UNKNOWN_MCAST_ACTION; 4101 4102 /* do not return rejected action frames */ 4103 if (mgmt->u.action.category & 0x80) 4104 return RX_DROP_U_REJECTED_ACTION_RESPONSE; 4105 4106 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0, 4107 GFP_ATOMIC); 4108 if (nskb) { 4109 struct ieee80211_mgmt *nmgmt = (void *)nskb->data; 4110 4111 nmgmt->u.action.category |= 0x80; 4112 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN); 4113 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN); 4114 4115 memset(nskb->cb, 0, sizeof(nskb->cb)); 4116 4117 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) { 4118 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb); 4119 4120 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN | 4121 IEEE80211_TX_INTFL_OFFCHAN_TX_OK | 4122 IEEE80211_TX_CTL_NO_CCK_RATE; 4123 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) 4124 info->hw_queue = 4125 local->hw.offchannel_tx_hw_queue; 4126 } 4127 4128 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7, -1, 4129 status->band); 4130 } 4131 4132 return RX_DROP_U_UNKNOWN_ACTION_REJECTED; 4133 } 4134 4135 static ieee80211_rx_result debug_noinline 4136 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx) 4137 { 4138 struct ieee80211_sub_if_data *sdata = rx->sdata; 4139 struct ieee80211_hdr *hdr = (void *)rx->skb->data; 4140 4141 if (!ieee80211_is_ext(hdr->frame_control)) 4142 return RX_CONTINUE; 4143 4144 if (sdata->vif.type != NL80211_IFTYPE_STATION) 4145 return RX_DROP_U_UNEXPECTED_EXT_FRAME; 4146 4147 /* for now only beacons are ext, so queue them */ 4148 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb); 4149 4150 return RX_QUEUED; 4151 } 4152 4153 static ieee80211_rx_result debug_noinline 4154 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx) 4155 { 4156 struct ieee80211_sub_if_data *sdata = rx->sdata; 4157 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data; 4158 __le16 stype; 4159 4160 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE); 4161 4162 if (!ieee80211_vif_is_mesh(&sdata->vif) && 4163 sdata->vif.type != NL80211_IFTYPE_ADHOC && 4164 sdata->vif.type != NL80211_IFTYPE_OCB && 4165 sdata->vif.type != NL80211_IFTYPE_STATION) 4166 return RX_DROP_U_UNHANDLED_MGMT; 4167 4168 switch (stype) { 4169 case cpu_to_le16(IEEE80211_STYPE_AUTH): 4170 case cpu_to_le16(IEEE80211_STYPE_BEACON): 4171 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP): 4172 /* process for all: mesh, mlme, ibss */ 4173 break; 4174 case cpu_to_le16(IEEE80211_STYPE_DEAUTH): 4175 if (is_multicast_ether_addr(mgmt->da) && 4176 !is_broadcast_ether_addr(mgmt->da)) 4177 return RX_DROP_U_MCAST_DEAUTH; 4178 4179 /* process only for station/IBSS */ 4180 if (sdata->vif.type != NL80211_IFTYPE_STATION && 4181 sdata->vif.type != NL80211_IFTYPE_ADHOC) 4182 return RX_DROP_U_UNHANDLED_DEAUTH; 4183 break; 4184 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP): 4185 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP): 4186 case cpu_to_le16(IEEE80211_STYPE_DISASSOC): 4187 if (is_multicast_ether_addr(mgmt->da) && 4188 !is_broadcast_ether_addr(mgmt->da)) 4189 return RX_DROP_U_MCAST_DISASSOC; 4190 4191 /* process only for station */ 4192 if (sdata->vif.type != NL80211_IFTYPE_STATION) 4193 return RX_DROP_U_UNHANDLED_DISASSOC; 4194 break; 4195 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ): 4196 /* process only for ibss and mesh */ 4197 if (sdata->vif.type != NL80211_IFTYPE_ADHOC && 4198 sdata->vif.type != NL80211_IFTYPE_MESH_POINT) 4199 return RX_DROP_U_UNHANDLED_PREQ; 4200 break; 4201 default: 4202 return RX_DROP_U_UNHANDLED_MGMT_STYPE; 4203 } 4204 4205 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb); 4206 4207 return RX_QUEUED; 4208 } 4209 4210 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx, 4211 ieee80211_rx_result res) 4212 { 4213 if (res == RX_QUEUED) { 4214 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued); 4215 return; 4216 } 4217 4218 if (res != RX_CONTINUE) { 4219 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop); 4220 if (rx->sta) 4221 rx->link_sta->rx_stats.dropped++; 4222 } 4223 4224 kfree_skb_reason(rx->skb, (__force u32)res); 4225 } 4226 4227 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx, 4228 struct sk_buff_head *frames) 4229 { 4230 ieee80211_rx_result res; 4231 struct sk_buff *skb; 4232 4233 #define CALL_RXH(rxh) \ 4234 do { \ 4235 res = rxh(rx); \ 4236 if (res != RX_CONTINUE) \ 4237 goto rxh_next; \ 4238 } while (0) 4239 4240 /* Lock here to avoid hitting all of the data used in the RX 4241 * path (e.g. key data, station data, ...) concurrently when 4242 * a frame is released from the reorder buffer due to timeout 4243 * from the timer, potentially concurrently with RX from the 4244 * driver. 4245 */ 4246 spin_lock_bh(&rx->local->rx_path_lock); 4247 4248 while ((skb = __skb_dequeue(frames))) { 4249 /* 4250 * all the other fields are valid across frames 4251 * that belong to an aMPDU since they are on the 4252 * same TID from the same station 4253 */ 4254 rx->skb = skb; 4255 4256 if (WARN_ON_ONCE(!rx->link)) { 4257 res = RX_DROP_U_NO_LINK; 4258 goto rxh_next; 4259 } 4260 4261 CALL_RXH(ieee80211_rx_h_check_more_data); 4262 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll); 4263 CALL_RXH(ieee80211_rx_h_sta_process); 4264 CALL_RXH(ieee80211_rx_h_decrypt); 4265 CALL_RXH(ieee80211_rx_h_defragment); 4266 CALL_RXH(ieee80211_rx_h_michael_mic_verify); 4267 /* must be after MMIC verify so header is counted in MPDU mic */ 4268 CALL_RXH(ieee80211_rx_h_amsdu); 4269 CALL_RXH(ieee80211_rx_h_data); 4270 4271 /* special treatment -- needs the queue */ 4272 res = ieee80211_rx_h_ctrl(rx, frames); 4273 if (res != RX_CONTINUE) 4274 goto rxh_next; 4275 4276 CALL_RXH(ieee80211_rx_h_mgmt_check); 4277 CALL_RXH(ieee80211_rx_h_action); 4278 CALL_RXH(ieee80211_rx_h_userspace_mgmt); 4279 CALL_RXH(ieee80211_rx_h_action_post_userspace); 4280 CALL_RXH(ieee80211_rx_h_action_return); 4281 CALL_RXH(ieee80211_rx_h_ext); 4282 CALL_RXH(ieee80211_rx_h_mgmt); 4283 4284 rxh_next: 4285 ieee80211_rx_handlers_result(rx, res); 4286 4287 #undef CALL_RXH 4288 } 4289 4290 spin_unlock_bh(&rx->local->rx_path_lock); 4291 } 4292 4293 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx) 4294 { 4295 struct sk_buff_head reorder_release; 4296 ieee80211_rx_result res; 4297 4298 __skb_queue_head_init(&reorder_release); 4299 4300 #define CALL_RXH(rxh) \ 4301 do { \ 4302 res = rxh(rx); \ 4303 if (res != RX_CONTINUE) \ 4304 goto rxh_next; \ 4305 } while (0) 4306 4307 CALL_RXH(ieee80211_rx_h_check_dup); 4308 CALL_RXH(ieee80211_rx_h_check); 4309 4310 ieee80211_rx_reorder_ampdu(rx, &reorder_release); 4311 4312 ieee80211_rx_handlers(rx, &reorder_release); 4313 return; 4314 4315 rxh_next: 4316 ieee80211_rx_handlers_result(rx, res); 4317 4318 #undef CALL_RXH 4319 } 4320 4321 static bool 4322 ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id) 4323 { 4324 return !!(sta->valid_links & BIT(link_id)); 4325 } 4326 4327 static bool ieee80211_rx_data_set_link(struct ieee80211_rx_data *rx, 4328 u8 link_id) 4329 { 4330 rx->link_id = link_id; 4331 rx->link = rcu_dereference(rx->sdata->link[link_id]); 4332 4333 if (!rx->sta) 4334 return rx->link; 4335 4336 if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id)) 4337 return false; 4338 4339 rx->link_sta = rcu_dereference(rx->sta->link[link_id]); 4340 4341 return rx->link && rx->link_sta; 4342 } 4343 4344 static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx, 4345 struct sta_info *sta, int link_id) 4346 { 4347 rx->link_id = link_id; 4348 rx->sta = sta; 4349 4350 if (sta) { 4351 rx->local = sta->sdata->local; 4352 if (!rx->sdata) 4353 rx->sdata = sta->sdata; 4354 rx->link_sta = &sta->deflink; 4355 } else { 4356 rx->link_sta = NULL; 4357 } 4358 4359 if (link_id < 0) { 4360 if (ieee80211_vif_is_mld(&rx->sdata->vif) && 4361 sta && !sta->sta.valid_links) 4362 rx->link = 4363 rcu_dereference(rx->sdata->link[sta->deflink.link_id]); 4364 else 4365 rx->link = &rx->sdata->deflink; 4366 } else if (!ieee80211_rx_data_set_link(rx, link_id)) { 4367 return false; 4368 } 4369 4370 return true; 4371 } 4372 4373 /* 4374 * This function makes calls into the RX path, therefore 4375 * it has to be invoked under RCU read lock. 4376 */ 4377 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid) 4378 { 4379 struct sk_buff_head frames; 4380 struct ieee80211_rx_data rx = { 4381 /* This is OK -- must be QoS data frame */ 4382 .security_idx = tid, 4383 .seqno_idx = tid, 4384 }; 4385 struct tid_ampdu_rx *tid_agg_rx; 4386 int link_id = -1; 4387 4388 /* FIXME: statistics won't be right with this */ 4389 if (sta->sta.valid_links) 4390 link_id = ffs(sta->sta.valid_links) - 1; 4391 4392 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id)) 4393 return; 4394 4395 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]); 4396 if (!tid_agg_rx) 4397 return; 4398 4399 __skb_queue_head_init(&frames); 4400 4401 spin_lock(&tid_agg_rx->reorder_lock); 4402 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames); 4403 spin_unlock(&tid_agg_rx->reorder_lock); 4404 4405 if (!skb_queue_empty(&frames)) { 4406 struct ieee80211_event event = { 4407 .type = BA_FRAME_TIMEOUT, 4408 .u.ba.tid = tid, 4409 .u.ba.sta = &sta->sta, 4410 }; 4411 drv_event_callback(rx.local, rx.sdata, &event); 4412 } 4413 4414 ieee80211_rx_handlers(&rx, &frames); 4415 } 4416 4417 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid, 4418 u16 ssn, u64 filtered, 4419 u16 received_mpdus) 4420 { 4421 struct ieee80211_local *local; 4422 struct sta_info *sta; 4423 struct tid_ampdu_rx *tid_agg_rx; 4424 struct sk_buff_head frames; 4425 struct ieee80211_rx_data rx = { 4426 /* This is OK -- must be QoS data frame */ 4427 .security_idx = tid, 4428 .seqno_idx = tid, 4429 }; 4430 int i, diff; 4431 4432 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS)) 4433 return; 4434 4435 __skb_queue_head_init(&frames); 4436 4437 sta = container_of(pubsta, struct sta_info, sta); 4438 4439 local = sta->sdata->local; 4440 WARN_ONCE(local->hw.max_rx_aggregation_subframes > 64, 4441 "RX BA marker can't support max_rx_aggregation_subframes %u > 64\n", 4442 local->hw.max_rx_aggregation_subframes); 4443 4444 if (!ieee80211_rx_data_set_sta(&rx, sta, -1)) 4445 return; 4446 4447 rcu_read_lock(); 4448 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]); 4449 if (!tid_agg_rx) 4450 goto out; 4451 4452 spin_lock_bh(&tid_agg_rx->reorder_lock); 4453 4454 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) { 4455 int release; 4456 4457 /* release all frames in the reorder buffer */ 4458 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) % 4459 IEEE80211_SN_MODULO; 4460 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, 4461 release, &frames); 4462 /* update ssn to match received ssn */ 4463 tid_agg_rx->head_seq_num = ssn; 4464 } else { 4465 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn, 4466 &frames); 4467 } 4468 4469 /* handle the case that received ssn is behind the mac ssn. 4470 * it can be tid_agg_rx->buf_size behind and still be valid */ 4471 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK; 4472 if (diff >= tid_agg_rx->buf_size) { 4473 tid_agg_rx->reorder_buf_filtered = 0; 4474 goto release; 4475 } 4476 filtered = filtered >> diff; 4477 ssn += diff; 4478 4479 /* update bitmap */ 4480 for (i = 0; i < tid_agg_rx->buf_size; i++) { 4481 int index = (ssn + i) % tid_agg_rx->buf_size; 4482 4483 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index); 4484 if (filtered & BIT_ULL(i)) 4485 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index); 4486 } 4487 4488 /* now process also frames that the filter marking released */ 4489 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames); 4490 4491 release: 4492 spin_unlock_bh(&tid_agg_rx->reorder_lock); 4493 4494 ieee80211_rx_handlers(&rx, &frames); 4495 4496 out: 4497 rcu_read_unlock(); 4498 } 4499 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames); 4500 4501 /* main receive path */ 4502 4503 static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr) 4504 { 4505 return ether_addr_equal(raddr, addr) || 4506 is_broadcast_ether_addr(raddr); 4507 } 4508 4509 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx) 4510 { 4511 struct ieee80211_sub_if_data *sdata = rx->sdata; 4512 struct sk_buff *skb = rx->skb; 4513 struct ieee80211_hdr *hdr = (void *)skb->data; 4514 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 4515 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type); 4516 bool multicast; 4517 static const u8 nan_network_id[ETH_ALEN] __aligned(2) = { 4518 0x51, 0x6F, 0x9A, 0x01, 0x00, 0x00 4519 }; 4520 4521 if (ieee80211_is_s1g_beacon(hdr->frame_control)) 4522 return sdata->vif.type == NL80211_IFTYPE_STATION && bssid; 4523 4524 multicast = is_multicast_ether_addr(hdr->addr1); 4525 4526 switch (sdata->vif.type) { 4527 case NL80211_IFTYPE_STATION: 4528 if (!bssid && !sdata->u.mgd.use_4addr) 4529 return false; 4530 if (ieee80211_is_first_frag(hdr->seq_ctrl) && 4531 ieee80211_is_robust_mgmt_frame(skb) && !rx->sta) 4532 return false; 4533 if (multicast) 4534 return true; 4535 return ieee80211_is_our_addr(sdata, hdr->addr1, &rx->link_id); 4536 case NL80211_IFTYPE_ADHOC: 4537 if (!bssid) 4538 return false; 4539 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) || 4540 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) || 4541 !is_valid_ether_addr(hdr->addr2)) 4542 return false; 4543 if (ieee80211_is_beacon(hdr->frame_control)) 4544 return true; 4545 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) 4546 return false; 4547 if (!multicast && 4548 !ether_addr_equal(sdata->vif.addr, hdr->addr1)) 4549 return false; 4550 if (!rx->sta) { 4551 int rate_idx; 4552 if (status->encoding != RX_ENC_LEGACY) 4553 rate_idx = 0; /* TODO: HT/VHT rates */ 4554 else 4555 rate_idx = status->rate_idx; 4556 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2, 4557 BIT(rate_idx)); 4558 } 4559 return true; 4560 case NL80211_IFTYPE_OCB: 4561 if (!bssid) 4562 return false; 4563 if (!ieee80211_is_data_present(hdr->frame_control)) 4564 return false; 4565 if (!is_broadcast_ether_addr(bssid)) 4566 return false; 4567 if (!multicast && 4568 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1)) 4569 return false; 4570 /* reject invalid/our STA address */ 4571 if (!is_valid_ether_addr(hdr->addr2) || 4572 ether_addr_equal(sdata->dev->dev_addr, hdr->addr2)) 4573 return false; 4574 if (!rx->sta) { 4575 int rate_idx; 4576 if (status->encoding != RX_ENC_LEGACY) 4577 rate_idx = 0; /* TODO: HT rates */ 4578 else 4579 rate_idx = status->rate_idx; 4580 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2, 4581 BIT(rate_idx)); 4582 } 4583 return true; 4584 case NL80211_IFTYPE_MESH_POINT: 4585 if (ether_addr_equal(sdata->vif.addr, hdr->addr2)) 4586 return false; 4587 if (multicast) 4588 return true; 4589 return ether_addr_equal(sdata->vif.addr, hdr->addr1); 4590 case NL80211_IFTYPE_AP_VLAN: 4591 case NL80211_IFTYPE_AP: 4592 if (!bssid) 4593 return ieee80211_is_our_addr(sdata, hdr->addr1, 4594 &rx->link_id); 4595 4596 if (!is_broadcast_ether_addr(bssid) && 4597 !ieee80211_is_our_addr(sdata, bssid, NULL)) { 4598 /* 4599 * Accept public action frames even when the 4600 * BSSID doesn't match, this is used for P2P 4601 * and location updates. Note that mac80211 4602 * itself never looks at these frames. 4603 */ 4604 if (!multicast && 4605 !ieee80211_is_our_addr(sdata, hdr->addr1, 4606 &rx->link_id)) 4607 return false; 4608 if (ieee80211_is_public_action(hdr, skb->len)) 4609 return true; 4610 return ieee80211_is_beacon(hdr->frame_control); 4611 } 4612 4613 if (!ieee80211_has_tods(hdr->frame_control)) { 4614 /* ignore data frames to TDLS-peers */ 4615 if (ieee80211_is_data(hdr->frame_control)) 4616 return false; 4617 /* ignore action frames to TDLS-peers */ 4618 if (ieee80211_is_action(hdr->frame_control) && 4619 !is_broadcast_ether_addr(bssid) && 4620 !ether_addr_equal(bssid, hdr->addr1)) 4621 return false; 4622 } 4623 4624 /* 4625 * 802.11-2016 Table 9-26 says that for data frames, A1 must be 4626 * the BSSID - we've checked that already but may have accepted 4627 * the wildcard (ff:ff:ff:ff:ff:ff). 4628 * 4629 * It also says: 4630 * The BSSID of the Data frame is determined as follows: 4631 * a) If the STA is contained within an AP or is associated 4632 * with an AP, the BSSID is the address currently in use 4633 * by the STA contained in the AP. 4634 * 4635 * So we should not accept data frames with an address that's 4636 * multicast. 4637 * 4638 * Accepting it also opens a security problem because stations 4639 * could encrypt it with the GTK and inject traffic that way. 4640 */ 4641 if (ieee80211_is_data(hdr->frame_control) && multicast) 4642 return false; 4643 4644 return true; 4645 case NL80211_IFTYPE_P2P_DEVICE: 4646 return ieee80211_is_public_action(hdr, skb->len) || 4647 ieee80211_is_probe_req(hdr->frame_control) || 4648 ieee80211_is_probe_resp(hdr->frame_control) || 4649 ieee80211_is_beacon(hdr->frame_control) || 4650 (ieee80211_is_auth(hdr->frame_control) && 4651 ether_addr_equal(sdata->vif.addr, hdr->addr1)); 4652 case NL80211_IFTYPE_NAN: 4653 if (ieee80211_has_tods(hdr->frame_control) || 4654 ieee80211_has_fromds(hdr->frame_control)) 4655 return false; 4656 4657 /* 4658 * Accept only frames that are addressed to the NAN cluster 4659 * (based on the Cluster ID). From these frames, accept only 4660 * - public action frames, 4661 * - authentication frames to the local address, and 4662 * - robust management frames except disassoc. 4663 */ 4664 if (!ether_addr_equal(sdata->u.nan.conf.cluster_id, hdr->addr3)) 4665 return false; 4666 if (ieee80211_is_public_action(hdr, skb->len)) 4667 return true; 4668 if (ieee80211_is_auth(hdr->frame_control) && 4669 ether_addr_equal(sdata->vif.addr, hdr->addr1)) 4670 return true; 4671 if (!ieee80211_is_disassoc(hdr->frame_control) && 4672 ieee80211_is_robust_mgmt_frame(skb)) 4673 return true; 4674 return false; 4675 case NL80211_IFTYPE_NAN_DATA: 4676 if (ieee80211_has_tods(hdr->frame_control) || 4677 ieee80211_has_fromds(hdr->frame_control)) 4678 return false; 4679 4680 if (ieee80211_is_data(hdr->frame_control)) { 4681 struct ieee80211_sub_if_data *nmi; 4682 4683 nmi = rcu_dereference(sdata->u.nan_data.nmi); 4684 if (!nmi) 4685 return false; 4686 4687 if (!ether_addr_equal(nmi->u.nan.conf.cluster_id, 4688 hdr->addr3)) 4689 return false; 4690 4691 return multicast || 4692 ether_addr_equal(sdata->vif.addr, hdr->addr1); 4693 } 4694 4695 /* Non-public action frames (unicast or multicast) */ 4696 if (ieee80211_is_action(hdr->frame_control) && 4697 !ieee80211_is_public_action(hdr, skb->len) && 4698 (ether_addr_equal(nan_network_id, hdr->addr1) || 4699 ether_addr_equal(sdata->vif.addr, hdr->addr1))) 4700 return true; 4701 4702 /* Unicast secure management frames */ 4703 return ether_addr_equal(sdata->vif.addr, hdr->addr1) && 4704 ieee80211_is_unicast_robust_mgmt_frame(skb); 4705 case NL80211_IFTYPE_PD: 4706 /* 4707 * Accept only authentication frames (PASN) addressed to 4708 * this interface. 4709 */ 4710 return ieee80211_is_auth(hdr->frame_control) && 4711 ether_addr_equal(sdata->vif.addr, hdr->addr1); 4712 default: 4713 break; 4714 } 4715 4716 WARN_ON_ONCE(1); 4717 return false; 4718 } 4719 4720 void ieee80211_check_fast_rx(struct sta_info *sta) 4721 { 4722 struct ieee80211_sub_if_data *sdata = sta->sdata; 4723 struct ieee80211_local *local = sdata->local; 4724 struct ieee80211_key *key; 4725 struct ieee80211_fast_rx fastrx = { 4726 .dev = sdata->dev, 4727 .vif_type = sdata->vif.type, 4728 .control_port_protocol = sdata->control_port_protocol, 4729 }, *old, *new = NULL; 4730 u32 offload_flags; 4731 bool set_offload = false; 4732 bool assign = false; 4733 bool offload; 4734 4735 /* use sparse to check that we don't return without updating */ 4736 __acquire(check_fast_rx); 4737 4738 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header)); 4739 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN); 4740 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header); 4741 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr); 4742 4743 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS); 4744 4745 /* fast-rx doesn't do reordering */ 4746 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) && 4747 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER)) 4748 goto clear; 4749 4750 switch (sdata->vif.type) { 4751 case NL80211_IFTYPE_STATION: 4752 if (sta->sta.tdls) { 4753 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1); 4754 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2); 4755 fastrx.expected_ds_bits = 0; 4756 } else { 4757 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1); 4758 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3); 4759 fastrx.expected_ds_bits = 4760 cpu_to_le16(IEEE80211_FCTL_FROMDS); 4761 } 4762 4763 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) { 4764 fastrx.expected_ds_bits |= 4765 cpu_to_le16(IEEE80211_FCTL_TODS); 4766 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3); 4767 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4); 4768 } 4769 4770 if (!sdata->u.mgd.powersave) 4771 break; 4772 4773 /* software powersave is a huge mess, avoid all of it */ 4774 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) 4775 goto clear; 4776 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) && 4777 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) 4778 goto clear; 4779 break; 4780 case NL80211_IFTYPE_AP_VLAN: 4781 case NL80211_IFTYPE_AP: 4782 /* parallel-rx requires this, at least with calls to 4783 * ieee80211_sta_ps_transition() 4784 */ 4785 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS)) 4786 goto clear; 4787 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3); 4788 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2); 4789 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS); 4790 4791 fastrx.internal_forward = 4792 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) && 4793 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || 4794 !sdata->u.vlan.sta); 4795 4796 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 4797 sdata->u.vlan.sta) { 4798 fastrx.expected_ds_bits |= 4799 cpu_to_le16(IEEE80211_FCTL_FROMDS); 4800 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4); 4801 fastrx.internal_forward = 0; 4802 } 4803 4804 break; 4805 case NL80211_IFTYPE_MESH_POINT: 4806 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_FROMDS | 4807 IEEE80211_FCTL_TODS); 4808 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3); 4809 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4); 4810 break; 4811 default: 4812 goto clear; 4813 } 4814 4815 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED)) 4816 goto clear; 4817 4818 rcu_read_lock(); 4819 key = rcu_dereference(sta->ptk[sta->ptk_idx]); 4820 if (!key) 4821 key = rcu_dereference(sdata->default_unicast_key); 4822 if (key) { 4823 switch (key->conf.cipher) { 4824 case WLAN_CIPHER_SUITE_TKIP: 4825 /* we don't want to deal with MMIC in fast-rx */ 4826 goto clear_rcu; 4827 case WLAN_CIPHER_SUITE_CCMP: 4828 case WLAN_CIPHER_SUITE_CCMP_256: 4829 case WLAN_CIPHER_SUITE_GCMP: 4830 case WLAN_CIPHER_SUITE_GCMP_256: 4831 break; 4832 default: 4833 /* We also don't want to deal with 4834 * WEP or cipher scheme. 4835 */ 4836 goto clear_rcu; 4837 } 4838 4839 fastrx.key = true; 4840 fastrx.icv_len = key->conf.icv_len; 4841 } 4842 4843 assign = true; 4844 clear_rcu: 4845 rcu_read_unlock(); 4846 clear: 4847 __release(check_fast_rx); 4848 4849 if (assign) 4850 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL); 4851 4852 offload_flags = get_bss_sdata(sdata)->vif.offload_flags; 4853 offload = offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED; 4854 4855 if (assign && offload) 4856 set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD); 4857 else 4858 set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD); 4859 4860 if (set_offload) 4861 drv_sta_set_decap_offload(local, sdata, &sta->sta, assign); 4862 4863 spin_lock_bh(&sta->lock); 4864 old = rcu_dereference_protected(sta->fast_rx, true); 4865 rcu_assign_pointer(sta->fast_rx, new); 4866 spin_unlock_bh(&sta->lock); 4867 4868 if (old) 4869 kfree_rcu(old, rcu_head); 4870 } 4871 4872 void ieee80211_clear_fast_rx(struct sta_info *sta) 4873 { 4874 struct ieee80211_fast_rx *old; 4875 4876 spin_lock_bh(&sta->lock); 4877 old = rcu_dereference_protected(sta->fast_rx, true); 4878 RCU_INIT_POINTER(sta->fast_rx, NULL); 4879 spin_unlock_bh(&sta->lock); 4880 4881 if (old) 4882 kfree_rcu(old, rcu_head); 4883 } 4884 4885 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata) 4886 { 4887 struct ieee80211_local *local = sdata->local; 4888 struct sta_info *sta; 4889 4890 lockdep_assert_wiphy(local->hw.wiphy); 4891 4892 list_for_each_entry(sta, &local->sta_list, list) { 4893 if (sdata != sta->sdata && 4894 (!sta->sdata->bss || sta->sdata->bss != sdata->bss)) 4895 continue; 4896 ieee80211_check_fast_rx(sta); 4897 } 4898 } 4899 4900 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata) 4901 { 4902 struct ieee80211_local *local = sdata->local; 4903 4904 lockdep_assert_wiphy(local->hw.wiphy); 4905 4906 __ieee80211_check_fast_rx_iface(sdata); 4907 } 4908 4909 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx, 4910 struct ieee80211_fast_rx *fast_rx, 4911 int orig_len) 4912 { 4913 struct ieee80211_sta_rx_stats *stats; 4914 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 4915 struct sta_info *sta = rx->sta; 4916 struct link_sta_info *link_sta; 4917 struct sk_buff *skb = rx->skb; 4918 void *sa = skb->data + ETH_ALEN; 4919 void *da = skb->data; 4920 4921 if (rx->link_id >= 0) { 4922 link_sta = rcu_dereference(sta->link[rx->link_id]); 4923 if (WARN_ON_ONCE(!link_sta)) { 4924 dev_kfree_skb(rx->skb); 4925 return; 4926 } 4927 } else { 4928 link_sta = &sta->deflink; 4929 } 4930 4931 stats = &link_sta->rx_stats; 4932 if (fast_rx->uses_rss) 4933 stats = this_cpu_ptr(link_sta->pcpu_rx_stats); 4934 4935 /* statistics part of ieee80211_rx_h_sta_process() */ 4936 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) { 4937 stats->last_signal = status->signal; 4938 if (!fast_rx->uses_rss) 4939 ewma_signal_add(&link_sta->rx_stats_avg.signal, 4940 -status->signal); 4941 } 4942 4943 if (status->chains) { 4944 int i; 4945 4946 stats->chains = status->chains; 4947 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) { 4948 int signal = status->chain_signal[i]; 4949 4950 if (!(status->chains & BIT(i))) 4951 continue; 4952 4953 stats->chain_signal_last[i] = signal; 4954 if (!fast_rx->uses_rss) 4955 ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i], 4956 -signal); 4957 } 4958 } 4959 /* end of statistics */ 4960 4961 stats->last_rx = jiffies; 4962 stats->last_rate = sta_stats_encode_rate(status); 4963 4964 stats->fragments++; 4965 stats->packets++; 4966 4967 skb->dev = fast_rx->dev; 4968 4969 dev_sw_netstats_rx_add(fast_rx->dev, skb->len); 4970 4971 /* The seqno index has the same property as needed 4972 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS 4973 * for non-QoS-data frames. Here we know it's a data 4974 * frame, so count MSDUs. 4975 */ 4976 u64_stats_update_begin(&stats->syncp); 4977 u64_stats_inc(&stats->msdu[rx->seqno_idx]); 4978 u64_stats_add(&stats->bytes, orig_len); 4979 u64_stats_update_end(&stats->syncp); 4980 4981 if (fast_rx->internal_forward) { 4982 struct sk_buff *xmit_skb = NULL; 4983 if (is_multicast_ether_addr(da)) { 4984 xmit_skb = skb_copy(skb, GFP_ATOMIC); 4985 } else if (!ether_addr_equal(da, sa) && 4986 sta_info_get(rx->sdata, da)) { 4987 xmit_skb = skb; 4988 skb = NULL; 4989 } 4990 4991 if (xmit_skb) { 4992 /* 4993 * Send to wireless media and increase priority by 256 4994 * to keep the received priority instead of 4995 * reclassifying the frame (see cfg80211_classify8021d). 4996 */ 4997 xmit_skb->priority += 256; 4998 xmit_skb->protocol = htons(ETH_P_802_3); 4999 skb_reset_network_header(xmit_skb); 5000 skb_reset_mac_header(xmit_skb); 5001 dev_queue_xmit(xmit_skb); 5002 } 5003 5004 if (!skb) 5005 return; 5006 } 5007 5008 /* deliver to local stack */ 5009 skb->protocol = eth_type_trans(skb, fast_rx->dev); 5010 ieee80211_deliver_skb_to_local_stack(skb, rx); 5011 } 5012 5013 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx, 5014 struct ieee80211_fast_rx *fast_rx) 5015 { 5016 struct sk_buff *skb = rx->skb; 5017 struct ieee80211_hdr *hdr = (void *)skb->data; 5018 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 5019 ieee80211_rx_result res; 5020 int orig_len = skb->len; 5021 int hdrlen = ieee80211_hdrlen(hdr->frame_control); 5022 int snap_offs = hdrlen; 5023 struct { 5024 u8 snap[sizeof(rfc1042_header)]; 5025 __be16 proto; 5026 } *payload __aligned(2); 5027 struct { 5028 u8 da[ETH_ALEN]; 5029 u8 sa[ETH_ALEN]; 5030 } addrs __aligned(2); 5031 struct ieee80211_sta_rx_stats *stats; 5032 u32 encoded_rate; 5033 5034 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write 5035 * to a common data structure; drivers can implement that per queue 5036 * but we don't have that information in mac80211 5037 */ 5038 if (!(status->flag & RX_FLAG_DUP_VALIDATED)) 5039 return false; 5040 5041 #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED) 5042 5043 /* If using encryption, we also need to have: 5044 * - PN_VALIDATED: similar, but the implementation is tricky 5045 * - DECRYPTED: necessary for PN_VALIDATED 5046 */ 5047 if (fast_rx->key && 5048 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS) 5049 return false; 5050 5051 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) 5052 return false; 5053 5054 if (unlikely(ieee80211_is_frag(hdr))) 5055 return false; 5056 5057 /* Since our interface address cannot be multicast, this 5058 * implicitly also rejects multicast frames without the 5059 * explicit check. 5060 * 5061 * We shouldn't get any *data* frames not addressed to us 5062 * (AP mode will accept multicast *management* frames), but 5063 * punting here will make it go through the full checks in 5064 * ieee80211_accept_frame(). 5065 */ 5066 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1)) 5067 return false; 5068 5069 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS | 5070 IEEE80211_FCTL_TODS)) != 5071 fast_rx->expected_ds_bits) 5072 return false; 5073 5074 /* assign the key to drop unencrypted frames (later) 5075 * and strip the IV/MIC if necessary 5076 */ 5077 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) { 5078 /* GCMP header length is the same */ 5079 snap_offs += IEEE80211_CCMP_HDR_LEN; 5080 } 5081 5082 if (!ieee80211_vif_is_mesh(&rx->sdata->vif) && 5083 !(status->rx_flags & IEEE80211_RX_AMSDU)) { 5084 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload))) 5085 return false; 5086 5087 payload = (void *)(skb->data + snap_offs); 5088 5089 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr)) 5090 return false; 5091 5092 /* Don't handle these here since they require special code. 5093 * Accept AARP and IPX even though they should come with a 5094 * bridge-tunnel header - but if we get them this way then 5095 * there's little point in discarding them. 5096 */ 5097 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) || 5098 payload->proto == fast_rx->control_port_protocol)) 5099 return false; 5100 } 5101 5102 /* after this point, don't punt to the slowpath! */ 5103 5104 if (fast_rx->uses_rss) 5105 stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats); 5106 else 5107 stats = &rx->link_sta->rx_stats; 5108 5109 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) && 5110 pskb_trim(skb, skb->len - fast_rx->icv_len)) 5111 goto drop; 5112 5113 if (rx->key && !ieee80211_has_protected(hdr->frame_control)) 5114 goto drop; 5115 5116 if (status->rx_flags & IEEE80211_RX_AMSDU) { 5117 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) != 5118 RX_QUEUED) 5119 goto drop; 5120 5121 return true; 5122 } 5123 5124 /* do the header conversion - first grab the addresses */ 5125 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs); 5126 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs); 5127 if (ieee80211_vif_is_mesh(&rx->sdata->vif)) { 5128 skb_pull(skb, snap_offs - 2); 5129 put_unaligned_be16(skb->len - 2, skb->data); 5130 } else { 5131 skb_postpull_rcsum(skb, skb->data + snap_offs, 5132 sizeof(rfc1042_header) + 2); 5133 5134 /* remove the SNAP but leave the ethertype */ 5135 skb_pull(skb, snap_offs + sizeof(rfc1042_header)); 5136 } 5137 /* push the addresses in front */ 5138 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs)); 5139 5140 /* capture before mesh forward may memset or free skb->cb */ 5141 encoded_rate = sta_stats_encode_rate(status); 5142 5143 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb); 5144 switch (res) { 5145 case RX_QUEUED: 5146 stats->last_rx = jiffies; 5147 stats->last_rate = encoded_rate; 5148 return true; 5149 case RX_CONTINUE: 5150 break; 5151 default: 5152 goto drop; 5153 } 5154 5155 ieee80211_rx_8023(rx, fast_rx, orig_len); 5156 5157 return true; 5158 drop: 5159 dev_kfree_skb(skb); 5160 5161 stats->dropped++; 5162 return true; 5163 } 5164 5165 /* 5166 * This function returns whether or not the SKB 5167 * was destined for RX processing or not, which, 5168 * if consume is true, is equivalent to whether 5169 * or not the skb was consumed. 5170 */ 5171 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx, 5172 struct sk_buff *skb, bool consume) 5173 { 5174 struct ieee80211_local *local = rx->local; 5175 struct ieee80211_sub_if_data *sdata = rx->sdata; 5176 struct ieee80211_hdr *hdr = (void *)skb->data; 5177 struct link_sta_info *link_sta = rx->link_sta; 5178 struct ieee80211_link_data *link = rx->link; 5179 5180 rx->skb = skb; 5181 5182 /* See if we can do fast-rx; if we have to copy we already lost, 5183 * so punt in that case. We should never have to deliver a data 5184 * frame to multiple interfaces anyway. 5185 * 5186 * We skip the ieee80211_accept_frame() call and do the necessary 5187 * checking inside ieee80211_invoke_fast_rx(). 5188 */ 5189 if (consume && rx->sta) { 5190 struct ieee80211_fast_rx *fast_rx; 5191 5192 fast_rx = rcu_dereference(rx->sta->fast_rx); 5193 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx)) 5194 return true; 5195 } 5196 5197 if (!ieee80211_accept_frame(rx)) 5198 return false; 5199 5200 if (!consume) { 5201 struct skb_shared_hwtstamps *shwt; 5202 5203 rx->skb = skb_copy(skb, GFP_ATOMIC); 5204 if (!rx->skb) { 5205 if (net_ratelimit()) 5206 wiphy_debug(local->hw.wiphy, 5207 "failed to copy skb for %s\n", 5208 sdata->name); 5209 return true; 5210 } 5211 5212 /* skb_copy() does not copy the hw timestamps, so copy it 5213 * explicitly 5214 */ 5215 shwt = skb_hwtstamps(rx->skb); 5216 shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp; 5217 5218 /* Update the hdr pointer to the new skb for translation below */ 5219 hdr = (struct ieee80211_hdr *)rx->skb->data; 5220 } 5221 5222 if (ieee80211_is_s1g_beacon(hdr->frame_control)) { 5223 ieee80211_invoke_rx_handlers(rx); 5224 return true; 5225 } 5226 5227 /* Store a copy of the pre-translated link addresses for SW crypto */ 5228 if (unlikely(is_unicast_ether_addr(hdr->addr1) && 5229 !ieee80211_is_data(hdr->frame_control))) 5230 memcpy(rx->link_addrs, &hdr->addrs, 3 * ETH_ALEN); 5231 5232 if (unlikely(rx->sta && rx->sta->sta.mlo) && 5233 is_unicast_ether_addr(hdr->addr1) && 5234 !ieee80211_is_probe_resp(hdr->frame_control) && 5235 !ieee80211_is_beacon(hdr->frame_control)) { 5236 /* translate to MLD addresses */ 5237 if (ether_addr_equal(link->conf->addr, hdr->addr1)) 5238 ether_addr_copy(hdr->addr1, rx->sdata->vif.addr); 5239 if (ether_addr_equal(link_sta->addr, hdr->addr2)) 5240 ether_addr_copy(hdr->addr2, rx->sta->addr); 5241 /* translate A3 only if it's the BSSID */ 5242 if (!ieee80211_has_tods(hdr->frame_control) && 5243 !ieee80211_has_fromds(hdr->frame_control)) { 5244 if (ether_addr_equal(link_sta->addr, hdr->addr3)) 5245 ether_addr_copy(hdr->addr3, rx->sta->addr); 5246 else if (ether_addr_equal(link->conf->addr, hdr->addr3)) 5247 ether_addr_copy(hdr->addr3, rx->sdata->vif.addr); 5248 } 5249 /* not needed for A4 since it can only carry the SA */ 5250 } 5251 5252 ieee80211_invoke_rx_handlers(rx); 5253 return true; 5254 } 5255 5256 static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw, 5257 struct ieee80211_sta *pubsta, 5258 struct sk_buff *skb, 5259 struct list_head *list) 5260 { 5261 struct ieee80211_local *local = hw_to_local(hw); 5262 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 5263 struct ieee80211_fast_rx *fast_rx; 5264 struct ieee80211_rx_data rx; 5265 struct sta_info *sta; 5266 int link_id = -1; 5267 5268 memset(&rx, 0, sizeof(rx)); 5269 rx.skb = skb; 5270 rx.local = local; 5271 rx.list = list; 5272 rx.link_id = -1; 5273 5274 I802_DEBUG_INC(local->dot11ReceivedFragmentCount); 5275 5276 /* drop frame if too short for header */ 5277 if (skb->len < sizeof(struct ethhdr)) 5278 goto drop; 5279 5280 if (!pubsta) 5281 goto drop; 5282 5283 if (status->link_valid) 5284 link_id = status->link_id; 5285 5286 /* 5287 * TODO: Should the frame be dropped if the right link_id is not 5288 * available? Or may be it is fine in the current form to proceed with 5289 * the frame processing because with frame being in 802.3 format, 5290 * link_id is used only for stats purpose and updating the stats on 5291 * the deflink is fine? 5292 */ 5293 sta = container_of(pubsta, struct sta_info, sta); 5294 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id)) 5295 goto drop; 5296 5297 fast_rx = rcu_dereference(rx.sta->fast_rx); 5298 if (!fast_rx) 5299 goto drop; 5300 5301 ieee80211_rx_8023(&rx, fast_rx, skb->len); 5302 return; 5303 5304 drop: 5305 dev_kfree_skb(skb); 5306 } 5307 5308 static bool ieee80211_rx_for_interface(struct ieee80211_rx_data *rx, 5309 struct sk_buff *skb, bool consume) 5310 { 5311 struct link_sta_info *link_sta; 5312 struct ieee80211_hdr *hdr = (void *)skb->data; 5313 struct sta_info *sta; 5314 int link_id = -1; 5315 5316 if (ieee80211_is_s1g_beacon(hdr->frame_control)) { 5317 if (!ieee80211_rx_data_set_sta(rx, NULL, -1)) 5318 return false; 5319 5320 return ieee80211_prepare_and_rx_handle(rx, skb, consume); 5321 } 5322 5323 /* 5324 * Look up link station first, in case there's a 5325 * chance that they might have a link address that 5326 * is identical to the MLD address, that way we'll 5327 * have the link information if needed. 5328 */ 5329 link_sta = link_sta_info_get_bss(rx->sdata, hdr->addr2); 5330 if (link_sta) { 5331 sta = link_sta->sta; 5332 link_id = link_sta->link_id; 5333 } else { 5334 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 5335 5336 sta = sta_info_get_bss(rx->sdata, hdr->addr2); 5337 if (status->link_valid) { 5338 link_id = status->link_id; 5339 } else if (ieee80211_vif_is_mld(&rx->sdata->vif) && 5340 status->freq) { 5341 struct ieee80211_link_data *link; 5342 struct ieee80211_chanctx_conf *conf; 5343 5344 for_each_link_data_rcu(rx->sdata, link) { 5345 conf = rcu_dereference(link->conf->chanctx_conf); 5346 if (!conf || !conf->def.chan) 5347 continue; 5348 5349 if (status->freq == conf->def.chan->center_freq) { 5350 link_id = link->link_id; 5351 break; 5352 } 5353 } 5354 } 5355 } 5356 5357 if (!ieee80211_rx_data_set_sta(rx, sta, link_id)) 5358 return false; 5359 5360 return ieee80211_prepare_and_rx_handle(rx, skb, consume); 5361 } 5362 5363 /* 5364 * This is the actual Rx frames handler. as it belongs to Rx path it must 5365 * be called with rcu_read_lock protection. 5366 */ 5367 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw, 5368 struct ieee80211_sta *pubsta, 5369 struct sk_buff *skb, 5370 struct list_head *list) 5371 { 5372 struct ieee80211_local *local = hw_to_local(hw); 5373 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 5374 struct ieee80211_sub_if_data *sdata; 5375 struct ieee80211_hdr *hdr; 5376 __le16 fc; 5377 struct ieee80211_rx_data rx; 5378 struct ieee80211_sub_if_data *prev; 5379 struct rhlist_head *tmp; 5380 int err = 0; 5381 5382 fc = ((struct ieee80211_hdr *)skb->data)->frame_control; 5383 memset(&rx, 0, sizeof(rx)); 5384 rx.skb = skb; 5385 rx.local = local; 5386 rx.list = list; 5387 rx.link_id = -1; 5388 5389 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc)) 5390 I802_DEBUG_INC(local->dot11ReceivedFragmentCount); 5391 5392 if (ieee80211_is_mgmt(fc)) { 5393 /* drop frame if too short for header */ 5394 if (skb->len < ieee80211_hdrlen(fc)) 5395 err = -ENOBUFS; 5396 else 5397 err = skb_linearize(skb); 5398 } else if (ieee80211_is_s1g_beacon(fc)) { 5399 size_t s1g_hdr_len = offsetof(struct ieee80211_ext, 5400 u.s1g_beacon.variable) + 5401 ieee80211_s1g_optional_len(fc); 5402 5403 if (skb->len < s1g_hdr_len) 5404 err = -ENOBUFS; 5405 else 5406 err = skb_linearize(skb); 5407 } else if (ieee80211_is_ext(fc)) { 5408 err = -EINVAL; 5409 } else { 5410 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc)); 5411 } 5412 5413 if (err) { 5414 dev_kfree_skb(skb); 5415 return; 5416 } 5417 5418 hdr = (struct ieee80211_hdr *)skb->data; 5419 ieee80211_parse_qos(&rx); 5420 ieee80211_verify_alignment(&rx); 5421 5422 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) || 5423 ieee80211_is_beacon(hdr->frame_control) || 5424 ieee80211_is_s1g_beacon(hdr->frame_control))) 5425 ieee80211_scan_rx(local, skb); 5426 5427 if (ieee80211_is_data(fc)) { 5428 struct sta_info *sta, *prev_sta; 5429 int link_id = -1; 5430 5431 if (status->link_valid) 5432 link_id = status->link_id; 5433 5434 if (pubsta) { 5435 sta = container_of(pubsta, struct sta_info, sta); 5436 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id)) 5437 goto out; 5438 5439 /* 5440 * In MLO connection, fetch the link_id using addr2 5441 * when the driver does not pass link_id in status. 5442 * When the address translation is already performed by 5443 * driver/hw, the valid link_id must be passed in 5444 * status. 5445 */ 5446 5447 if (!status->link_valid && pubsta->mlo) { 5448 struct link_sta_info *link_sta; 5449 5450 link_sta = link_sta_info_get_bss(rx.sdata, 5451 hdr->addr2); 5452 if (!link_sta) 5453 goto out; 5454 5455 if (!ieee80211_rx_data_set_link(&rx, 5456 link_sta->link_id)) 5457 goto out; 5458 } 5459 5460 if (ieee80211_prepare_and_rx_handle(&rx, skb, true)) 5461 return; 5462 goto out; 5463 } 5464 5465 prev_sta = NULL; 5466 5467 for_each_sta_info(local, hdr->addr2, sta, tmp) { 5468 if (!prev_sta) { 5469 prev_sta = sta; 5470 continue; 5471 } 5472 5473 rx.sdata = prev_sta->sdata; 5474 if (!status->link_valid && prev_sta->sta.mlo) { 5475 struct link_sta_info *link_sta; 5476 5477 link_sta = link_sta_info_get_bss(rx.sdata, 5478 hdr->addr2); 5479 if (!link_sta) 5480 continue; 5481 5482 link_id = link_sta->link_id; 5483 } 5484 5485 if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id)) 5486 goto out; 5487 5488 ieee80211_prepare_and_rx_handle(&rx, skb, false); 5489 5490 prev_sta = sta; 5491 } 5492 5493 if (prev_sta) { 5494 rx.sdata = prev_sta->sdata; 5495 if (!status->link_valid && prev_sta->sta.mlo) { 5496 struct link_sta_info *link_sta; 5497 5498 link_sta = link_sta_info_get_bss(rx.sdata, 5499 hdr->addr2); 5500 if (!link_sta) 5501 goto out; 5502 5503 link_id = link_sta->link_id; 5504 } 5505 5506 if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id)) 5507 goto out; 5508 5509 if (ieee80211_prepare_and_rx_handle(&rx, skb, true)) 5510 return; 5511 goto out; 5512 } 5513 } 5514 5515 prev = NULL; 5516 5517 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 5518 if (!ieee80211_sdata_running(sdata)) 5519 continue; 5520 5521 if (sdata->vif.type == NL80211_IFTYPE_MONITOR || 5522 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 5523 continue; 5524 5525 /* 5526 * frame is destined for this interface, but if it's 5527 * not also for the previous one we handle that after 5528 * the loop to avoid copying the SKB once too much 5529 */ 5530 5531 if (!prev) { 5532 prev = sdata; 5533 continue; 5534 } 5535 5536 rx.sdata = prev; 5537 ieee80211_rx_for_interface(&rx, skb, false); 5538 5539 prev = sdata; 5540 } 5541 5542 if (prev) { 5543 rx.sdata = prev; 5544 5545 if (ieee80211_rx_for_interface(&rx, skb, true)) 5546 return; 5547 } 5548 5549 out: 5550 dev_kfree_skb(skb); 5551 } 5552 5553 /* 5554 * This is the receive path handler. It is called by a low level driver when an 5555 * 802.11 MPDU is received from the hardware. 5556 */ 5557 void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta, 5558 struct sk_buff *skb, struct list_head *list) 5559 { 5560 struct ieee80211_local *local = hw_to_local(hw); 5561 struct ieee80211_rate *rate = NULL; 5562 struct ieee80211_supported_band *sband; 5563 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 5564 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 5565 5566 WARN_ON_ONCE(softirq_count() == 0); 5567 5568 if (WARN_ON(status->band >= NUM_NL80211_BANDS)) 5569 goto drop; 5570 5571 sband = local->hw.wiphy->bands[status->band]; 5572 if (WARN_ON(!sband)) 5573 goto drop; 5574 5575 /* 5576 * If we're suspending, it is possible although not too likely 5577 * that we'd be receiving frames after having already partially 5578 * quiesced the stack. We can't process such frames then since 5579 * that might, for example, cause stations to be added or other 5580 * driver callbacks be invoked. 5581 */ 5582 if (unlikely(local->quiescing || local->suspended)) 5583 goto drop; 5584 5585 /* We might be during a HW reconfig, prevent Rx for the same reason */ 5586 if (unlikely(local->in_reconfig)) 5587 goto drop; 5588 5589 /* 5590 * The same happens when we're not even started, 5591 * but that's worth a warning. 5592 */ 5593 if (WARN_ON(!local->started)) 5594 goto drop; 5595 5596 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC) && 5597 !(status->flag & RX_FLAG_NO_PSDU && 5598 status->zero_length_psdu_type == 5599 IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED))) { 5600 /* 5601 * Validate the rate, unless there was a PLCP error which may 5602 * have an invalid rate or the PSDU was not capture and may be 5603 * missing rate information. 5604 */ 5605 5606 switch (status->encoding) { 5607 case RX_ENC_HT: 5608 /* 5609 * rate_idx is MCS index, which can be [0-76] 5610 * as documented on: 5611 * 5612 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n 5613 * 5614 * Anything else would be some sort of driver or 5615 * hardware error. The driver should catch hardware 5616 * errors. 5617 */ 5618 if (WARN(status->rate_idx > 76, 5619 "Rate marked as an HT rate but passed " 5620 "status->rate_idx is not " 5621 "an MCS index [0-76]: %d (0x%02x)\n", 5622 status->rate_idx, 5623 status->rate_idx)) 5624 goto drop; 5625 break; 5626 case RX_ENC_VHT: 5627 if (WARN_ONCE(status->rate_idx > 11 || 5628 !status->nss || 5629 status->nss > 8, 5630 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n", 5631 status->rate_idx, status->nss)) 5632 goto drop; 5633 break; 5634 case RX_ENC_HE: 5635 if (WARN_ONCE(status->rate_idx > 11 || 5636 !status->nss || 5637 status->nss > 8, 5638 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n", 5639 status->rate_idx, status->nss)) 5640 goto drop; 5641 break; 5642 case RX_ENC_EHT: 5643 if (WARN_ONCE(status->rate_idx > 15 || 5644 !status->nss || 5645 status->nss > 8 || 5646 status->eht.gi > NL80211_RATE_INFO_EHT_GI_3_2, 5647 "Rate marked as an EHT rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n", 5648 status->rate_idx, status->nss, status->eht.gi)) 5649 goto drop; 5650 break; 5651 case RX_ENC_UHR: 5652 if (WARN_ONCE(!(status->rate_idx <= 15 || 5653 status->rate_idx == 17 || 5654 status->rate_idx == 19 || 5655 status->rate_idx == 20 || 5656 status->rate_idx == 23) || 5657 !status->nss || 5658 status->nss > 8 || 5659 status->uhr.gi > NL80211_RATE_INFO_EHT_GI_3_2, 5660 "Rate marked as a UHR rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n", 5661 status->rate_idx, status->nss, status->uhr.gi)) 5662 goto drop; 5663 if (WARN_ONCE(status->uhr.elr && 5664 (status->nss != 1 || status->rate_idx > 1 || 5665 status->uhr.gi != NL80211_RATE_INFO_EHT_GI_1_6 || 5666 status->bw != RATE_INFO_BW_20 || status->uhr.im), 5667 "bad UHR ELR MCS MCS:%d, NSS:%d, GI:%d, BW:%d, IM:%d\n", 5668 status->rate_idx, status->nss, status->uhr.gi, 5669 status->bw, status->uhr.im)) 5670 goto drop; 5671 if (WARN_ONCE(status->uhr.im && 5672 (status->nss != 1 || status->rate_idx == 15), 5673 "bad UHR IM MCS MCS:%d, NSS:%d\n", 5674 status->rate_idx, status->nss)) 5675 goto drop; 5676 break; 5677 case RX_ENC_S1G: 5678 if (WARN_ONCE(status->rate_idx > 12 || 5679 !status->nss || 5680 status->nss > 4, 5681 "Rate marked as an S1G rate but data is invalid: MCS: %d, NSS: %d\n", 5682 status->rate_idx, status->nss)) 5683 goto drop; 5684 break; 5685 default: 5686 WARN_ON_ONCE(1); 5687 fallthrough; 5688 case RX_ENC_LEGACY: 5689 if (WARN_ON(status->rate_idx >= sband->n_bitrates)) 5690 goto drop; 5691 rate = &sband->bitrates[status->rate_idx]; 5692 } 5693 } 5694 5695 if (WARN_ON_ONCE(status->link_id >= IEEE80211_LINK_UNSPECIFIED)) 5696 goto drop; 5697 5698 status->rx_flags = 0; 5699 5700 kcov_remote_start_common(skb_get_kcov_handle(skb)); 5701 5702 /* 5703 * Frames with failed FCS/PLCP checksum are not returned, 5704 * all other frames are returned without radiotap header 5705 * if it was previously present. 5706 * Also, frames with less than 16 bytes are dropped. 5707 */ 5708 if (!(status->flag & RX_FLAG_8023)) 5709 skb = ieee80211_rx_monitor(local, skb, rate); 5710 if (skb) { 5711 if ((status->flag & RX_FLAG_8023) || 5712 ieee80211_is_data_present(hdr->frame_control)) 5713 ieee80211_tpt_led_trig_rx(local, skb->len); 5714 5715 if (status->flag & RX_FLAG_8023) 5716 __ieee80211_rx_handle_8023(hw, pubsta, skb, list); 5717 else 5718 __ieee80211_rx_handle_packet(hw, pubsta, skb, list); 5719 } 5720 5721 kcov_remote_stop(); 5722 return; 5723 drop: 5724 kfree_skb(skb); 5725 } 5726 EXPORT_SYMBOL(ieee80211_rx_list); 5727 5728 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta, 5729 struct sk_buff *skb, struct napi_struct *napi) 5730 { 5731 struct sk_buff *tmp; 5732 LIST_HEAD(list); 5733 5734 5735 /* 5736 * key references and virtual interfaces are protected using RCU 5737 * and this requires that we are in a read-side RCU section during 5738 * receive processing 5739 */ 5740 rcu_read_lock(); 5741 ieee80211_rx_list(hw, pubsta, skb, &list); 5742 rcu_read_unlock(); 5743 5744 if (!napi) { 5745 netif_receive_skb_list(&list); 5746 return; 5747 } 5748 5749 list_for_each_entry_safe(skb, tmp, &list, list) { 5750 skb_list_del_init(skb); 5751 napi_gro_receive(napi, skb); 5752 } 5753 } 5754 EXPORT_SYMBOL(ieee80211_rx_napi); 5755 5756 /* This is a version of the rx handler that can be called from hard irq 5757 * context. Post the skb on the queue and schedule the tasklet */ 5758 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb) 5759 { 5760 struct ieee80211_local *local = hw_to_local(hw); 5761 5762 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb)); 5763 5764 skb->pkt_type = IEEE80211_RX_MSG; 5765 skb_queue_tail(&local->skb_queue, skb); 5766 tasklet_schedule(&local->tasklet); 5767 } 5768 EXPORT_SYMBOL(ieee80211_rx_irqsafe); 5769