1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * NXP Wireless LAN device driver: AP TX and RX data handling 4 * 5 * Copyright 2011-2020 NXP 6 */ 7 8 #include "decl.h" 9 #include "ioctl.h" 10 #include "main.h" 11 #include "wmm.h" 12 #include "11n_aggr.h" 13 #include "11n_rxreorder.h" 14 15 /* This function checks if particular RA list has packets more than low bridge 16 * packet threshold and then deletes packet from this RA list. 17 * Function deletes packets from such RA list and returns true. If no such list 18 * is found, false is returned. 19 */ 20 static bool 21 mwifiex_uap_del_tx_pkts_in_ralist(struct mwifiex_private *priv, 22 struct list_head *ra_list_head, 23 int tid) 24 { 25 struct mwifiex_ra_list_tbl *ra_list; 26 struct sk_buff *skb, *tmp; 27 bool pkt_deleted = false; 28 struct mwifiex_txinfo *tx_info; 29 struct mwifiex_adapter *adapter = priv->adapter; 30 31 list_for_each_entry(ra_list, ra_list_head, list) { 32 if (skb_queue_empty(&ra_list->skb_head)) 33 continue; 34 35 skb_queue_walk_safe(&ra_list->skb_head, skb, tmp) { 36 tx_info = MWIFIEX_SKB_TXCB(skb); 37 if (tx_info->flags & MWIFIEX_BUF_FLAG_BRIDGED_PKT) { 38 __skb_unlink(skb, &ra_list->skb_head); 39 mwifiex_write_data_complete(adapter, skb, 0, 40 -1); 41 if (ra_list->tx_paused) 42 priv->wmm.pkts_paused[tid]--; 43 else 44 atomic_dec(&priv->wmm.tx_pkts_queued); 45 pkt_deleted = true; 46 } 47 if ((atomic_read(&adapter->pending_bridged_pkts) <= 48 MWIFIEX_BRIDGED_PKTS_THR_LOW)) 49 break; 50 } 51 } 52 53 return pkt_deleted; 54 } 55 56 /* This function deletes packets from particular RA List. RA list index 57 * from which packets are deleted is preserved so that packets from next RA 58 * list are deleted upon subsequent call thus maintaining fairness. 59 */ 60 static void mwifiex_uap_cleanup_tx_queues(struct mwifiex_private *priv) 61 { 62 struct list_head *ra_list; 63 int i; 64 65 spin_lock_bh(&priv->wmm.ra_list_spinlock); 66 67 for (i = 0; i < MAX_NUM_TID; i++, priv->del_list_idx++) { 68 if (priv->del_list_idx == MAX_NUM_TID) 69 priv->del_list_idx = 0; 70 ra_list = &priv->wmm.tid_tbl_ptr[priv->del_list_idx].ra_list; 71 if (mwifiex_uap_del_tx_pkts_in_ralist(priv, ra_list, i)) { 72 priv->del_list_idx++; 73 break; 74 } 75 } 76 77 spin_unlock_bh(&priv->wmm.ra_list_spinlock); 78 } 79 80 81 static void mwifiex_uap_queue_bridged_pkt(struct mwifiex_private *priv, 82 struct sk_buff *skb) 83 { 84 struct mwifiex_adapter *adapter = priv->adapter; 85 struct uap_rxpd *uap_rx_pd; 86 struct rx_packet_hdr *rx_pkt_hdr; 87 struct sk_buff *new_skb; 88 struct mwifiex_txinfo *tx_info; 89 int hdr_chop; 90 struct ethhdr *p_ethhdr; 91 struct mwifiex_sta_node *src_node; 92 int index; 93 94 uap_rx_pd = (struct uap_rxpd *)(skb->data); 95 rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset); 96 97 if ((atomic_read(&adapter->pending_bridged_pkts) >= 98 MWIFIEX_BRIDGED_PKTS_THR_HIGH)) { 99 mwifiex_dbg(priv->adapter, ERROR, 100 "Tx: Bridge packet limit reached. Drop packet!\n"); 101 kfree_skb(skb); 102 mwifiex_uap_cleanup_tx_queues(priv); 103 return; 104 } 105 106 if (sizeof(*rx_pkt_hdr) + 107 le16_to_cpu(uap_rx_pd->rx_pkt_offset) > skb->len) { 108 mwifiex_dbg(adapter, ERROR, 109 "wrong rx packet offset: len=%d,rx_pkt_offset=%d\n", 110 skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset)); 111 priv->stats.rx_dropped++; 112 dev_kfree_skb_any(skb); 113 return; 114 } 115 116 if ((!memcmp(&rx_pkt_hdr->rfc1042_hdr, bridge_tunnel_header, 117 sizeof(bridge_tunnel_header))) || 118 (!memcmp(&rx_pkt_hdr->rfc1042_hdr, rfc1042_header, 119 sizeof(rfc1042_header)) && 120 ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_AARP && 121 ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_IPX)) { 122 /* Replace the 803 header and rfc1042 header (llc/snap) with 123 * an Ethernet II header, keep the src/dst and snap_type 124 * (ethertype). 125 * 126 * The firmware only passes up SNAP frames converting all RX 127 * data from 802.11 to 802.2/LLC/SNAP frames. 128 * 129 * To create the Ethernet II, just move the src, dst address 130 * right before the snap_type. 131 */ 132 p_ethhdr = (struct ethhdr *) 133 ((u8 *)(&rx_pkt_hdr->eth803_hdr) 134 + sizeof(rx_pkt_hdr->eth803_hdr) 135 + sizeof(rx_pkt_hdr->rfc1042_hdr) 136 - sizeof(rx_pkt_hdr->eth803_hdr.h_dest) 137 - sizeof(rx_pkt_hdr->eth803_hdr.h_source) 138 - sizeof(rx_pkt_hdr->rfc1042_hdr.snap_type)); 139 memcpy(p_ethhdr->h_source, rx_pkt_hdr->eth803_hdr.h_source, 140 sizeof(p_ethhdr->h_source)); 141 memcpy(p_ethhdr->h_dest, rx_pkt_hdr->eth803_hdr.h_dest, 142 sizeof(p_ethhdr->h_dest)); 143 /* Chop off the rxpd + the excess memory from 144 * 802.2/llc/snap header that was removed. 145 */ 146 hdr_chop = (u8 *)p_ethhdr - (u8 *)uap_rx_pd; 147 } else { 148 /* Chop off the rxpd */ 149 hdr_chop = (u8 *)&rx_pkt_hdr->eth803_hdr - (u8 *)uap_rx_pd; 150 } 151 152 /* Chop off the leading header bytes so that it points 153 * to the start of either the reconstructed EthII frame 154 * or the 802.2/llc/snap frame. 155 */ 156 skb_pull(skb, hdr_chop); 157 158 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) { 159 mwifiex_dbg(priv->adapter, ERROR, 160 "data: Tx: insufficient skb headroom %d\n", 161 skb_headroom(skb)); 162 /* Insufficient skb headroom - allocate a new skb */ 163 new_skb = 164 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN); 165 if (unlikely(!new_skb)) { 166 mwifiex_dbg(priv->adapter, ERROR, 167 "Tx: cannot allocate new_skb\n"); 168 kfree_skb(skb); 169 priv->stats.tx_dropped++; 170 return; 171 } 172 173 kfree_skb(skb); 174 skb = new_skb; 175 mwifiex_dbg(priv->adapter, INFO, 176 "info: new skb headroom %d\n", 177 skb_headroom(skb)); 178 } 179 180 tx_info = MWIFIEX_SKB_TXCB(skb); 181 memset(tx_info, 0, sizeof(*tx_info)); 182 tx_info->bss_num = priv->bss_num; 183 tx_info->bss_type = priv->bss_type; 184 tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT; 185 186 src_node = mwifiex_get_sta_entry(priv, rx_pkt_hdr->eth803_hdr.h_source); 187 if (src_node) { 188 src_node->stats.last_rx = jiffies; 189 src_node->stats.rx_bytes += skb->len; 190 src_node->stats.rx_packets++; 191 src_node->stats.last_tx_rate = uap_rx_pd->rx_rate; 192 src_node->stats.last_tx_htinfo = uap_rx_pd->ht_info; 193 } 194 195 if (is_unicast_ether_addr(rx_pkt_hdr->eth803_hdr.h_dest)) { 196 /* Update bridge packet statistics as the 197 * packet is not going to kernel/upper layer. 198 */ 199 priv->stats.rx_bytes += skb->len; 200 priv->stats.rx_packets++; 201 202 /* Sending bridge packet to TX queue, so save the packet 203 * length in TXCB to update statistics in TX complete. 204 */ 205 tx_info->pkt_len = skb->len; 206 } 207 208 __net_timestamp(skb); 209 210 index = mwifiex_1d_to_wmm_queue[skb->priority]; 211 atomic_inc(&priv->wmm_tx_pending[index]); 212 mwifiex_wmm_add_buf_txqueue(priv, skb); 213 atomic_inc(&adapter->tx_pending); 214 atomic_inc(&adapter->pending_bridged_pkts); 215 216 mwifiex_queue_main_work(priv->adapter); 217 218 return; 219 } 220 221 /* 222 * This function contains logic for AP packet forwarding. 223 * 224 * If a packet is multicast/broadcast, it is sent to kernel/upper layer 225 * as well as queued back to AP TX queue so that it can be sent to other 226 * associated stations. 227 * If a packet is unicast and RA is present in associated station list, 228 * it is again requeued into AP TX queue. 229 * If a packet is unicast and RA is not in associated station list, 230 * packet is forwarded to kernel to handle routing logic. 231 */ 232 int mwifiex_handle_uap_rx_forward(struct mwifiex_private *priv, 233 struct sk_buff *skb) 234 { 235 struct mwifiex_adapter *adapter = priv->adapter; 236 struct uap_rxpd *uap_rx_pd; 237 struct rx_packet_hdr *rx_pkt_hdr; 238 u8 ra[ETH_ALEN]; 239 struct sk_buff *skb_uap; 240 241 uap_rx_pd = (struct uap_rxpd *)(skb->data); 242 rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset); 243 244 /* don't do packet forwarding in disconnected state */ 245 if (!priv->media_connected) { 246 mwifiex_dbg(adapter, ERROR, 247 "drop packet in disconnected state.\n"); 248 dev_kfree_skb_any(skb); 249 return 0; 250 } 251 252 memcpy(ra, rx_pkt_hdr->eth803_hdr.h_dest, ETH_ALEN); 253 254 if (is_multicast_ether_addr(ra)) { 255 skb_uap = skb_copy(skb, GFP_ATOMIC); 256 mwifiex_uap_queue_bridged_pkt(priv, skb_uap); 257 } else { 258 if (mwifiex_get_sta_entry(priv, ra)) { 259 /* Requeue Intra-BSS packet */ 260 mwifiex_uap_queue_bridged_pkt(priv, skb); 261 return 0; 262 } 263 } 264 265 /* Forward unicat/Inter-BSS packets to kernel. */ 266 return mwifiex_process_rx_packet(priv, skb); 267 } 268 269 int mwifiex_uap_recv_packet(struct mwifiex_private *priv, 270 struct sk_buff *skb) 271 { 272 struct mwifiex_adapter *adapter = priv->adapter; 273 struct mwifiex_sta_node *src_node; 274 struct ethhdr *p_ethhdr; 275 struct sk_buff *skb_uap; 276 struct mwifiex_txinfo *tx_info; 277 278 if (!skb) 279 return -1; 280 281 p_ethhdr = (void *)skb->data; 282 src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source); 283 if (src_node) { 284 src_node->stats.last_rx = jiffies; 285 src_node->stats.rx_bytes += skb->len; 286 src_node->stats.rx_packets++; 287 } 288 289 if (is_multicast_ether_addr(p_ethhdr->h_dest) || 290 mwifiex_get_sta_entry(priv, p_ethhdr->h_dest)) { 291 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) 292 skb_uap = 293 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN); 294 else 295 skb_uap = skb_copy(skb, GFP_ATOMIC); 296 297 if (likely(skb_uap)) { 298 tx_info = MWIFIEX_SKB_TXCB(skb_uap); 299 memset(tx_info, 0, sizeof(*tx_info)); 300 tx_info->bss_num = priv->bss_num; 301 tx_info->bss_type = priv->bss_type; 302 tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT; 303 __net_timestamp(skb_uap); 304 mwifiex_wmm_add_buf_txqueue(priv, skb_uap); 305 atomic_inc(&adapter->tx_pending); 306 atomic_inc(&adapter->pending_bridged_pkts); 307 if ((atomic_read(&adapter->pending_bridged_pkts) >= 308 MWIFIEX_BRIDGED_PKTS_THR_HIGH)) { 309 mwifiex_dbg(adapter, ERROR, 310 "Tx: Bridge packet limit reached. Drop packet!\n"); 311 mwifiex_uap_cleanup_tx_queues(priv); 312 } 313 314 } else { 315 mwifiex_dbg(adapter, ERROR, "failed to allocate skb_uap"); 316 } 317 318 mwifiex_queue_main_work(adapter); 319 /* Don't forward Intra-BSS unicast packet to upper layer*/ 320 if (mwifiex_get_sta_entry(priv, p_ethhdr->h_dest)) 321 return 0; 322 } 323 324 skb->dev = priv->netdev; 325 skb->protocol = eth_type_trans(skb, priv->netdev); 326 skb->ip_summed = CHECKSUM_NONE; 327 328 /* This is required only in case of 11n and USB/PCIE as we alloc 329 * a buffer of 4K only if its 11N (to be able to receive 4K 330 * AMSDU packets). In case of SD we allocate buffers based 331 * on the size of packet and hence this is not needed. 332 * 333 * Modifying the truesize here as our allocation for each 334 * skb is 4K but we only receive 2K packets and this cause 335 * the kernel to start dropping packets in case where 336 * application has allocated buffer based on 2K size i.e. 337 * if there a 64K packet received (in IP fragments and 338 * application allocates 64K to receive this packet but 339 * this packet would almost double up because we allocate 340 * each 1.5K fragment in 4K and pass it up. As soon as the 341 * 64K limit hits kernel will start to drop rest of the 342 * fragments. Currently we fail the Filesndl-ht.scr script 343 * for UDP, hence this fix 344 */ 345 if ((adapter->iface_type == MWIFIEX_USB || 346 adapter->iface_type == MWIFIEX_PCIE) && 347 skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE) 348 skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE); 349 350 /* Forward multicast/broadcast packet to upper layer*/ 351 netif_rx(skb); 352 return 0; 353 } 354 355 /* 356 * This function processes the packet received on AP interface. 357 * 358 * The function looks into the RxPD and performs sanity tests on the 359 * received buffer to ensure its a valid packet before processing it 360 * further. If the packet is determined to be aggregated, it is 361 * de-aggregated accordingly. Then skb is passed to AP packet forwarding logic. 362 * 363 * The completion callback is called after processing is complete. 364 */ 365 int mwifiex_process_uap_rx_packet(struct mwifiex_private *priv, 366 struct sk_buff *skb) 367 { 368 struct mwifiex_adapter *adapter = priv->adapter; 369 int ret; 370 struct uap_rxpd *uap_rx_pd; 371 struct rx_packet_hdr *rx_pkt_hdr; 372 u16 rx_pkt_type; 373 u8 ta[ETH_ALEN], pkt_type; 374 struct mwifiex_sta_node *node; 375 376 uap_rx_pd = (struct uap_rxpd *)(skb->data); 377 rx_pkt_type = le16_to_cpu(uap_rx_pd->rx_pkt_type); 378 rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset); 379 380 if (le16_to_cpu(uap_rx_pd->rx_pkt_offset) + 381 sizeof(rx_pkt_hdr->eth803_hdr) > skb->len) { 382 mwifiex_dbg(adapter, ERROR, 383 "wrong rx packet for struct ethhdr: len=%d, offset=%d\n", 384 skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset)); 385 priv->stats.rx_dropped++; 386 dev_kfree_skb_any(skb); 387 return 0; 388 } 389 390 ether_addr_copy(ta, rx_pkt_hdr->eth803_hdr.h_source); 391 392 if ((le16_to_cpu(uap_rx_pd->rx_pkt_offset) + 393 le16_to_cpu(uap_rx_pd->rx_pkt_length)) > (u16) skb->len) { 394 mwifiex_dbg(adapter, ERROR, 395 "wrong rx packet: len=%d, offset=%d, length=%d\n", 396 skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset), 397 le16_to_cpu(uap_rx_pd->rx_pkt_length)); 398 priv->stats.rx_dropped++; 399 400 node = mwifiex_get_sta_entry(priv, ta); 401 if (node) 402 node->stats.tx_failed++; 403 404 dev_kfree_skb_any(skb); 405 return 0; 406 } 407 408 if (rx_pkt_type == PKT_TYPE_MGMT) { 409 ret = mwifiex_process_mgmt_packet(priv, skb); 410 if (ret) 411 mwifiex_dbg(adapter, DATA, "Rx of mgmt packet failed"); 412 dev_kfree_skb_any(skb); 413 return ret; 414 } 415 416 417 if (rx_pkt_type != PKT_TYPE_BAR && uap_rx_pd->priority < MAX_NUM_TID) { 418 spin_lock_bh(&priv->sta_list_spinlock); 419 node = mwifiex_get_sta_entry(priv, ta); 420 if (node) 421 node->rx_seq[uap_rx_pd->priority] = 422 le16_to_cpu(uap_rx_pd->seq_num); 423 spin_unlock_bh(&priv->sta_list_spinlock); 424 } 425 426 if (!priv->ap_11n_enabled || 427 (!mwifiex_11n_get_rx_reorder_tbl(priv, uap_rx_pd->priority, ta) && 428 (le16_to_cpu(uap_rx_pd->rx_pkt_type) != PKT_TYPE_AMSDU))) { 429 ret = mwifiex_handle_uap_rx_forward(priv, skb); 430 return ret; 431 } 432 433 /* Reorder and send to kernel */ 434 pkt_type = (u8)le16_to_cpu(uap_rx_pd->rx_pkt_type); 435 ret = mwifiex_11n_rx_reorder_pkt(priv, le16_to_cpu(uap_rx_pd->seq_num), 436 uap_rx_pd->priority, ta, pkt_type, 437 skb); 438 439 if (ret || (rx_pkt_type == PKT_TYPE_BAR)) 440 dev_kfree_skb_any(skb); 441 442 if (ret) 443 priv->stats.rx_dropped++; 444 445 return ret; 446 } 447 448 /* 449 * This function fills the TxPD for AP tx packets. 450 * 451 * The Tx buffer received by this function should already have the 452 * header space allocated for TxPD. 453 * 454 * This function inserts the TxPD in between interface header and actual 455 * data and adjusts the buffer pointers accordingly. 456 * 457 * The following TxPD fields are set by this function, as required - 458 * - BSS number 459 * - Tx packet length and offset 460 * - Priority 461 * - Packet delay 462 * - Priority specific Tx control 463 * - Flags 464 */ 465 void mwifiex_process_uap_txpd(struct mwifiex_private *priv, 466 struct sk_buff *skb) 467 { 468 struct mwifiex_adapter *adapter = priv->adapter; 469 struct uap_txpd *txpd; 470 struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb); 471 int pad; 472 u16 pkt_type, pkt_offset; 473 int hroom = adapter->intf_hdr_len; 474 475 pkt_type = mwifiex_is_skb_mgmt_frame(skb) ? PKT_TYPE_MGMT : 0; 476 477 pad = ((uintptr_t)skb->data - (sizeof(*txpd) + hroom)) & 478 (MWIFIEX_DMA_ALIGN_SZ - 1); 479 480 skb_push(skb, sizeof(*txpd) + pad); 481 482 txpd = (struct uap_txpd *)skb->data; 483 memset(txpd, 0, sizeof(*txpd)); 484 txpd->bss_num = priv->bss_num; 485 txpd->bss_type = priv->bss_type; 486 txpd->tx_pkt_length = cpu_to_le16((u16)(skb->len - (sizeof(*txpd) + 487 pad))); 488 txpd->priority = (u8)skb->priority; 489 490 txpd->pkt_delay_2ms = mwifiex_wmm_compute_drv_pkt_delay(priv, skb); 491 492 if (tx_info->flags & MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS || 493 tx_info->flags & MWIFIEX_BUF_FLAG_ACTION_TX_STATUS) { 494 txpd->tx_token_id = tx_info->ack_frame_id; 495 txpd->flags |= MWIFIEX_TXPD_FLAGS_REQ_TX_STATUS; 496 } 497 498 if (txpd->priority < ARRAY_SIZE(priv->wmm.user_pri_pkt_tx_ctrl)) 499 /* 500 * Set the priority specific tx_control field, setting of 0 will 501 * cause the default value to be used later in this function. 502 */ 503 txpd->tx_control = 504 cpu_to_le32(priv->wmm.user_pri_pkt_tx_ctrl[txpd->priority]); 505 506 /* Offset of actual data */ 507 pkt_offset = sizeof(*txpd) + pad; 508 if (pkt_type == PKT_TYPE_MGMT) { 509 /* Set the packet type and add header for management frame */ 510 txpd->tx_pkt_type = cpu_to_le16(pkt_type); 511 pkt_offset += MWIFIEX_MGMT_FRAME_HEADER_SIZE; 512 } 513 514 txpd->tx_pkt_offset = cpu_to_le16(pkt_offset); 515 516 /* make space for adapter->intf_hdr_len */ 517 skb_push(skb, hroom); 518 519 if (!txpd->tx_control) 520 /* TxCtrl set by user or default */ 521 txpd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl); 522 } 523