rx.c 7.13 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4
/*
 * This file contains the handling of RX in wlan driver.
 */
5 6 7

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

8
#include <linux/etherdevice.h>
9
#include <linux/hardirq.h>
10
#include <linux/slab.h>
11
#include <linux/types.h>
12
#include <linux/export.h>
Kiran Divekar's avatar
Kiran Divekar committed
13
#include <net/cfg80211.h>
14

Kiran Divekar's avatar
Kiran Divekar committed
15
#include "defs.h"
16
#include "host.h"
17 18 19
#include "radiotap.h"
#include "decl.h"
#include "dev.h"
20
#include "mesh.h"
21 22 23 24 25

struct eth803hdr {
	u8 dest_addr[6];
	u8 src_addr[6];
	u16 h803_len;
26
} __packed;
27 28 29 30 31 32 33

struct rfc1042hdr {
	u8 llc_dsap;
	u8 llc_ssap;
	u8 llc_ctrl;
	u8 snap_oui[3];
	u16 snap_type;
34
} __packed;
35 36 37 38

struct rxpackethdr {
	struct eth803hdr eth803_hdr;
	struct rfc1042hdr rfc1042_hdr;
39
} __packed;
40 41 42 43

struct rx80211packethdr {
	struct rxpd rx_pd;
	void *eth80211_hdr;
44
} __packed;
45

46 47
static int process_rxed_802_11_packet(struct lbs_private *priv,
	struct sk_buff *skb);
48 49

/**
50 51
 * lbs_process_rxed_packet - processes received packet and forwards it
 * to kernel/upper layer
52
 *
53 54 55
 * @priv:	A pointer to &struct lbs_private
 * @skb:	A pointer to skb which includes the received packet
 * returns:	0 or -1
56
 */
57
int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
58 59
{
	int ret = 0;
60
	struct net_device *dev = priv->dev;
61 62 63 64 65
	struct rxpackethdr *p_rx_pkt;
	struct rxpd *p_rx_pd;
	int hdrchop;
	struct ethhdr *p_ethhdr;

66 67
	BUG_ON(!skb);

68 69
	skb->ip_summed = CHECKSUM_NONE;

70 71 72 73
	if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
		ret = process_rxed_802_11_packet(priv, skb);
		goto done;
	}
74

75 76 77
	p_rx_pd = (struct rxpd *) skb->data;
	p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
		le32_to_cpu(p_rx_pd->pkt_ptr));
78 79

	dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
80

81
	lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
82 83 84
		 min_t(unsigned int, skb->len, 100));

	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
85
		lbs_deb_rx("rx err: frame received with bad length\n");
86
		dev->stats.rx_length_errors++;
87
		ret = -EINVAL;
88
		dev_kfree_skb(skb);
89 90 91
		goto done;
	}

92
	lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
93 94
		skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
		skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
95

96
	lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
97
		sizeof(p_rx_pkt->eth803_hdr.dest_addr));
98
	lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
99 100 101
		sizeof(p_rx_pkt->eth803_hdr.src_addr));

	if (memcmp(&p_rx_pkt->rfc1042_hdr,
102
		   rfc1042_header, sizeof(rfc1042_header)) == 0) {
103 104 105 106 107 108 109 110 111 112 113
		/*
		 *  Replace the 803 header and rfc1042 header (llc/snap) with an
		 *    EthernetII header, keep the src/dst and snap_type (ethertype)
		 *
		 *  The firmware only passes up SNAP frames converting
		 *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
		 *
		 *  To create the Ethernet II, just move the src, dst address right
		 *    before the snap_type.
		 */
		p_ethhdr = (struct ethhdr *)
114
		    ((u8 *) &p_rx_pkt->eth803_hdr
115 116 117 118 119 120 121 122 123 124 125 126 127
		     + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
		     - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
		     - sizeof(p_rx_pkt->eth803_hdr.src_addr)
		     - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));

		memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
		       sizeof(p_ethhdr->h_source));
		memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
		       sizeof(p_ethhdr->h_dest));

		/* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
		 *   that was removed
		 */
128
		hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
129
	} else {
130
		lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
131
			(u8 *) &p_rx_pkt->rfc1042_hdr,
132 133 134
			sizeof(p_rx_pkt->rfc1042_hdr));

		/* Chop off the rxpd */
135
		hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
136 137 138 139 140 141 142
	}

	/* Chop off the leading header bytes so the skb points to the start of
	 *   either the reconstructed EthII frame or the 802.2/llc/snap frame
	 */
	skb_pull(skb, hdrchop);

Kiran Divekar's avatar
Kiran Divekar committed
143
	priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
144

145
	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
146 147
	dev->stats.rx_bytes += skb->len;
	dev->stats.rx_packets++;
148

149
	skb->protocol = eth_type_trans(skb, dev);
150
	netif_rx_any_context(skb);
151

152 153 154 155
	ret = 0;
done:
	return ret;
}
156
EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
157 158

/**
159 160
 * convert_mv_rate_to_radiotap - converts Tx/Rx rates from Marvell WLAN format
 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
161
 *
162 163
 * @rate:	Input rate
 * returns:	Output Rate (0 if invalid)
164 165 166 167 168 169 170 171 172 173 174 175
 */
static u8 convert_mv_rate_to_radiotap(u8 rate)
{
	switch (rate) {
	case 0:		/*   1 Mbps */
		return 2;
	case 1:		/*   2 Mbps */
		return 4;
	case 2:		/* 5.5 Mbps */
		return 11;
	case 3:		/*  11 Mbps */
		return 22;
176 177
	/* case 4: reserved */
	case 5:		/*   6 Mbps */
178
		return 12;
179
	case 6:		/*   9 Mbps */
180
		return 18;
181
	case 7:		/*  12 Mbps */
182
		return 24;
183
	case 8:		/*  18 Mbps */
184
		return 36;
185
	case 9:		/*  24 Mbps */
186
		return 48;
187
	case 10:		/*  36 Mbps */
188
		return 72;
189
	case 11:		/*  48 Mbps */
190
		return 96;
191
	case 12:		/*  54 Mbps */
192 193
		return 108;
	}
194
	pr_alert("Invalid Marvell WLAN rate %i\n", rate);
195 196 197 198
	return 0;
}

/**
199 200
 * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
 * it to kernel/upper layer
201
 *
202 203 204
 * @priv:	A pointer to &struct lbs_private
 * @skb:	A pointer to skb which includes the received packet
 * returns:	0 or -1
205
 */
206 207
static int process_rxed_802_11_packet(struct lbs_private *priv,
	struct sk_buff *skb)
208 209
{
	int ret = 0;
210
	struct net_device *dev = priv->dev;
211 212 213 214 215 216 217 218
	struct rx80211packethdr *p_rx_pkt;
	struct rxpd *prxpd;
	struct rx_radiotap_hdr radiotap_hdr;
	struct rx_radiotap_hdr *pradiotap_hdr;

	p_rx_pkt = (struct rx80211packethdr *) skb->data;
	prxpd = &p_rx_pkt->rx_pd;

219
	/* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
220 221

	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
222
		lbs_deb_rx("rx err: frame received with bad length\n");
223
		dev->stats.rx_length_errors++;
224
		ret = -EINVAL;
225
		kfree_skb(skb);
226 227 228
		goto done;
	}

229
	lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
230 231 232 233
	       skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));

	/* create the exported radio header */

234
	/* radiotap header */
235 236
	memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
	/* XXX must check radiotap_hdr.hdr.it_pad for pad */
237 238 239 240 241 242 243 244 245 246 247
	radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
	radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
	radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
	/* XXX must check no carryout */
	radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;

	/* chop the rxpd */
	skb_pull(skb, sizeof(struct rxpd));

	/* add space for the new radio header */
	if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
248
	    pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
249
		netdev_alert(dev, "%s: couldn't pskb_expand_head\n", __func__);
250 251 252
		ret = -ENOMEM;
		kfree_skb(skb);
		goto done;
253 254
	}

255
	pradiotap_hdr = skb_push(skb, sizeof(struct rx_radiotap_hdr));
256 257
	memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));

Kiran Divekar's avatar
Kiran Divekar committed
258
	priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
259

260
	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
261 262
	dev->stats.rx_bytes += skb->len;
	dev->stats.rx_packets++;
263

Kiran Divekar's avatar
Kiran Divekar committed
264
	skb->protocol = eth_type_trans(skb, priv->dev);
265
	netif_rx_any_context(skb);
266

267 268
	ret = 0;

269 270
done:
	return ret;
271
}