1 /*
2  * This file contains the handling of RX in wlan driver.
3  */
4 
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 
7 #include <linux/etherdevice.h>
8 #include <linux/hardirq.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <linux/export.h>
12 #include <net/cfg80211.h>
13 
14 #include "defs.h"
15 #include "host.h"
16 #include "radiotap.h"
17 #include "decl.h"
18 #include "dev.h"
19 #include "mesh.h"
20 
21 struct eth803hdr {
22 	u8 dest_addr[6];
23 	u8 src_addr[6];
24 	u16 h803_len;
25 } __packed;
26 
27 struct rfc1042hdr {
28 	u8 llc_dsap;
29 	u8 llc_ssap;
30 	u8 llc_ctrl;
31 	u8 snap_oui[3];
32 	u16 snap_type;
33 } __packed;
34 
35 struct rxpackethdr {
36 	struct eth803hdr eth803_hdr;
37 	struct rfc1042hdr rfc1042_hdr;
38 } __packed;
39 
40 struct rx80211packethdr {
41 	struct rxpd rx_pd;
42 	void *eth80211_hdr;
43 } __packed;
44 
45 static int process_rxed_802_11_packet(struct lbs_private *priv,
46 	struct sk_buff *skb);
47 
48 /**
49  * lbs_process_rxed_packet - processes received packet and forwards it
50  * to kernel/upper layer
51  *
52  * @priv:	A pointer to &struct lbs_private
53  * @skb:	A pointer to skb which includes the received packet
54  * returns:	0 or -1
55  */
56 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
57 {
58 	int ret = 0;
59 	struct net_device *dev = priv->dev;
60 	struct rxpackethdr *p_rx_pkt;
61 	struct rxpd *p_rx_pd;
62 	int hdrchop;
63 	struct ethhdr *p_ethhdr;
64 	static const u8 rfc1042_eth_hdr[] = {
65 		0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
66 	};
67 
68 	lbs_deb_enter(LBS_DEB_RX);
69 
70 	BUG_ON(!skb);
71 
72 	skb->ip_summed = CHECKSUM_NONE;
73 
74 	if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
75 		ret = process_rxed_802_11_packet(priv, skb);
76 		goto done;
77 	}
78 
79 	p_rx_pd = (struct rxpd *) skb->data;
80 	p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
81 		le32_to_cpu(p_rx_pd->pkt_ptr));
82 
83 	dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
84 
85 	lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
86 		 min_t(unsigned int, skb->len, 100));
87 
88 	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
89 		lbs_deb_rx("rx err: frame received with bad length\n");
90 		dev->stats.rx_length_errors++;
91 		ret = -EINVAL;
92 		dev_kfree_skb(skb);
93 		goto done;
94 	}
95 
96 	lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
97 		skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
98 		skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
99 
100 	lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
101 		sizeof(p_rx_pkt->eth803_hdr.dest_addr));
102 	lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
103 		sizeof(p_rx_pkt->eth803_hdr.src_addr));
104 
105 	if (memcmp(&p_rx_pkt->rfc1042_hdr,
106 		   rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
107 		/*
108 		 *  Replace the 803 header and rfc1042 header (llc/snap) with an
109 		 *    EthernetII header, keep the src/dst and snap_type (ethertype)
110 		 *
111 		 *  The firmware only passes up SNAP frames converting
112 		 *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
113 		 *
114 		 *  To create the Ethernet II, just move the src, dst address right
115 		 *    before the snap_type.
116 		 */
117 		p_ethhdr = (struct ethhdr *)
118 		    ((u8 *) &p_rx_pkt->eth803_hdr
119 		     + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
120 		     - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
121 		     - sizeof(p_rx_pkt->eth803_hdr.src_addr)
122 		     - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
123 
124 		memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
125 		       sizeof(p_ethhdr->h_source));
126 		memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
127 		       sizeof(p_ethhdr->h_dest));
128 
129 		/* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
130 		 *   that was removed
131 		 */
132 		hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
133 	} else {
134 		lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
135 			(u8 *) &p_rx_pkt->rfc1042_hdr,
136 			sizeof(p_rx_pkt->rfc1042_hdr));
137 
138 		/* Chop off the rxpd */
139 		hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
140 	}
141 
142 	/* Chop off the leading header bytes so the skb points to the start of
143 	 *   either the reconstructed EthII frame or the 802.2/llc/snap frame
144 	 */
145 	skb_pull(skb, hdrchop);
146 
147 	priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
148 
149 	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
150 	dev->stats.rx_bytes += skb->len;
151 	dev->stats.rx_packets++;
152 
153 	skb->protocol = eth_type_trans(skb, dev);
154 	if (in_interrupt())
155 		netif_rx(skb);
156 	else
157 		netif_rx_ni(skb);
158 
159 	ret = 0;
160 done:
161 	lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
162 	return ret;
163 }
164 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
165 
166 /**
167  * convert_mv_rate_to_radiotap - converts Tx/Rx rates from Marvell WLAN format
168  * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
169  *
170  * @rate:	Input rate
171  * returns:	Output Rate (0 if invalid)
172  */
173 static u8 convert_mv_rate_to_radiotap(u8 rate)
174 {
175 	switch (rate) {
176 	case 0:		/*   1 Mbps */
177 		return 2;
178 	case 1:		/*   2 Mbps */
179 		return 4;
180 	case 2:		/* 5.5 Mbps */
181 		return 11;
182 	case 3:		/*  11 Mbps */
183 		return 22;
184 	/* case 4: reserved */
185 	case 5:		/*   6 Mbps */
186 		return 12;
187 	case 6:		/*   9 Mbps */
188 		return 18;
189 	case 7:		/*  12 Mbps */
190 		return 24;
191 	case 8:		/*  18 Mbps */
192 		return 36;
193 	case 9:		/*  24 Mbps */
194 		return 48;
195 	case 10:		/*  36 Mbps */
196 		return 72;
197 	case 11:		/*  48 Mbps */
198 		return 96;
199 	case 12:		/*  54 Mbps */
200 		return 108;
201 	}
202 	pr_alert("Invalid Marvell WLAN rate %i\n", rate);
203 	return 0;
204 }
205 
206 /**
207  * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
208  * it to kernel/upper layer
209  *
210  * @priv:	A pointer to &struct lbs_private
211  * @skb:	A pointer to skb which includes the received packet
212  * returns:	0 or -1
213  */
214 static int process_rxed_802_11_packet(struct lbs_private *priv,
215 	struct sk_buff *skb)
216 {
217 	int ret = 0;
218 	struct net_device *dev = priv->dev;
219 	struct rx80211packethdr *p_rx_pkt;
220 	struct rxpd *prxpd;
221 	struct rx_radiotap_hdr radiotap_hdr;
222 	struct rx_radiotap_hdr *pradiotap_hdr;
223 
224 	lbs_deb_enter(LBS_DEB_RX);
225 
226 	p_rx_pkt = (struct rx80211packethdr *) skb->data;
227 	prxpd = &p_rx_pkt->rx_pd;
228 
229 	/* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
230 
231 	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
232 		lbs_deb_rx("rx err: frame received with bad length\n");
233 		dev->stats.rx_length_errors++;
234 		ret = -EINVAL;
235 		kfree_skb(skb);
236 		goto done;
237 	}
238 
239 	lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
240 	       skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
241 
242 	/* create the exported radio header */
243 
244 	/* radiotap header */
245 	memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
246 	/* XXX must check radiotap_hdr.hdr.it_pad for pad */
247 	radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
248 	radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
249 	radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
250 	/* XXX must check no carryout */
251 	radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
252 
253 	/* chop the rxpd */
254 	skb_pull(skb, sizeof(struct rxpd));
255 
256 	/* add space for the new radio header */
257 	if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
258 	    pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
259 		netdev_alert(dev, "%s: couldn't pskb_expand_head\n", __func__);
260 		ret = -ENOMEM;
261 		kfree_skb(skb);
262 		goto done;
263 	}
264 
265 	pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
266 	memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
267 
268 	priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
269 
270 	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
271 	dev->stats.rx_bytes += skb->len;
272 	dev->stats.rx_packets++;
273 
274 	skb->protocol = eth_type_trans(skb, priv->dev);
275 
276 	if (in_interrupt())
277 		netif_rx(skb);
278 	else
279 		netif_rx_ni(skb);
280 
281 	ret = 0;
282 
283 done:
284 	lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
285 	return ret;
286 }
287