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