Merge tag 'mtd/for-5.7' of git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux
[linux-2.6-microblaze.git] / drivers / staging / wfx / data_rx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Datapath implementation.
4  *
5  * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  */
8 #include <linux/etherdevice.h>
9 #include <net/mac80211.h>
10
11 #include "data_rx.h"
12 #include "wfx.h"
13 #include "bh.h"
14 #include "sta.h"
15
16 static int wfx_drop_encrypt_data(struct wfx_dev *wdev,
17                                  const struct hif_ind_rx *arg,
18                                  struct sk_buff *skb)
19 {
20         struct ieee80211_hdr *frame = (struct ieee80211_hdr *)skb->data;
21         size_t hdrlen = ieee80211_hdrlen(frame->frame_control);
22         size_t iv_len, icv_len;
23
24         /* Oops... There is no fast way to ask mac80211 about
25          * IV/ICV lengths. Even defineas are not exposed.
26          */
27         switch (arg->rx_flags.encryp) {
28         case HIF_RI_FLAGS_WEP_ENCRYPTED:
29                 iv_len = 4 /* WEP_IV_LEN */;
30                 icv_len = 4 /* WEP_ICV_LEN */;
31                 break;
32         case HIF_RI_FLAGS_TKIP_ENCRYPTED:
33                 iv_len = 8 /* TKIP_IV_LEN */;
34                 icv_len = 4 /* TKIP_ICV_LEN */
35                         + 8 /*MICHAEL_MIC_LEN*/;
36                 break;
37         case HIF_RI_FLAGS_AES_ENCRYPTED:
38                 iv_len = 8 /* CCMP_HDR_LEN */;
39                 icv_len = 8 /* CCMP_MIC_LEN */;
40                 break;
41         case HIF_RI_FLAGS_WAPI_ENCRYPTED:
42                 iv_len = 18 /* WAPI_HDR_LEN */;
43                 icv_len = 16 /* WAPI_MIC_LEN */;
44                 break;
45         default:
46                 dev_err(wdev->dev, "unknown encryption type %d\n",
47                         arg->rx_flags.encryp);
48                 return -EIO;
49         }
50
51         /* Firmware strips ICV in case of MIC failure. */
52         if (arg->status == HIF_STATUS_MICFAILURE)
53                 icv_len = 0;
54
55         if (skb->len < hdrlen + iv_len + icv_len) {
56                 dev_warn(wdev->dev, "malformed SDU received\n");
57                 return -EIO;
58         }
59
60         /* Remove IV, ICV and MIC */
61         skb_trim(skb, skb->len - icv_len);
62         memmove(skb->data + iv_len, skb->data, hdrlen);
63         skb_pull(skb, iv_len);
64         return 0;
65 }
66
67 void wfx_rx_cb(struct wfx_vif *wvif,
68                const struct hif_ind_rx *arg, struct sk_buff *skb)
69 {
70         struct ieee80211_rx_status *hdr = IEEE80211_SKB_RXCB(skb);
71         struct ieee80211_hdr *frame = (struct ieee80211_hdr *)skb->data;
72         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
73
74         memset(hdr, 0, sizeof(*hdr));
75
76         // FIXME: Why do we drop these frames?
77         if (!arg->rcpi_rssi &&
78             (ieee80211_is_probe_resp(frame->frame_control) ||
79              ieee80211_is_beacon(frame->frame_control)))
80                 goto drop;
81
82         if (arg->status == HIF_STATUS_MICFAILURE)
83                 hdr->flag |= RX_FLAG_MMIC_ERROR;
84         else if (arg->status)
85                 goto drop;
86
87         if (skb->len < sizeof(struct ieee80211_pspoll)) {
88                 dev_warn(wvif->wdev->dev, "malformed SDU received\n");
89                 goto drop;
90         }
91
92         hdr->band = NL80211_BAND_2GHZ;
93         hdr->freq = ieee80211_channel_to_frequency(arg->channel_number,
94                                                    hdr->band);
95
96         if (arg->rxed_rate >= 14) {
97                 hdr->encoding = RX_ENC_HT;
98                 hdr->rate_idx = arg->rxed_rate - 14;
99         } else if (arg->rxed_rate >= 4) {
100                 hdr->rate_idx = arg->rxed_rate - 2;
101         } else {
102                 hdr->rate_idx = arg->rxed_rate;
103         }
104
105         hdr->signal = arg->rcpi_rssi / 2 - 110;
106         hdr->antenna = 0;
107
108         if (arg->rx_flags.encryp) {
109                 if (wfx_drop_encrypt_data(wvif->wdev, arg, skb))
110                         goto drop;
111                 hdr->flag |= RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED;
112                 if (arg->rx_flags.encryp == HIF_RI_FLAGS_TKIP_ENCRYPTED)
113                         hdr->flag |= RX_FLAG_MMIC_STRIPPED;
114         }
115
116         /* Filter block ACK negotiation: fully controlled by firmware */
117         if (ieee80211_is_action(frame->frame_control) &&
118             arg->rx_flags.match_uc_addr &&
119             mgmt->u.action.category == WLAN_CATEGORY_BACK)
120                 goto drop;
121         if (ieee80211_is_beacon(frame->frame_control) &&
122             !arg->status && wvif->vif &&
123             ether_addr_equal(ieee80211_get_SA(frame),
124                              wvif->vif->bss_conf.bssid)) {
125                 /* Disable beacon filter once we're associated... */
126                 if (wvif->disable_beacon_filter &&
127                     (wvif->vif->bss_conf.assoc ||
128                      wvif->vif->bss_conf.ibss_joined)) {
129                         wvif->disable_beacon_filter = false;
130                         schedule_work(&wvif->update_filtering_work);
131                 }
132         }
133         ieee80211_rx_irqsafe(wvif->wdev->hw, skb);
134
135         return;
136
137 drop:
138         dev_kfree_skb(skb);
139 }