blob: 3ba5eabdb583adeba8e0972c060d1d6582cdefac [file] [log] [blame]
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -07001/* src/p80211/p80211conv.c
2*
3* Ether/802.11 conversions and packet buffer routines
4*
5* Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
6* --------------------------------------------------------------------
7*
8* linux-wlan
9*
10* The contents of this file are subject to the Mozilla Public
11* License Version 1.1 (the "License"); you may not use this file
12* except in compliance with the License. You may obtain a copy of
13* the License at http://www.mozilla.org/MPL/
14*
15* Software distributed under the License is distributed on an "AS
16* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17* implied. See the License for the specific language governing
18* rights and limitations under the License.
19*
20* Alternatively, the contents of this file may be used under the
21* terms of the GNU Public License version 2 (the "GPL"), in which
22* case the provisions of the GPL are applicable instead of the
23* above. If you wish to allow the use of your version of this file
24* only under the terms of the GPL and not to allow others to use
25* your version of this file under the MPL, indicate your decision
26* by deleting the provisions above and replace them with the notice
27* and other provisions required by the GPL. If you do not delete
28* the provisions above, a recipient may use your version of this
29* file under either the MPL or the GPL.
30*
31* --------------------------------------------------------------------
32*
33* Inquiries regarding the linux-wlan Open Source project can be
34* made directly to:
35*
36* AbsoluteValue Systems Inc.
37* info@linux-wlan.com
38* http://www.linux-wlan.com
39*
40* --------------------------------------------------------------------
41*
42* Portions of the development of this software were funded by
43* Intersil Corporation as part of PRISM(R) chipset product development.
44*
45* --------------------------------------------------------------------
46*
47* This file defines the functions that perform Ethernet to/from
48* 802.11 frame conversions.
49*
50* --------------------------------------------------------------------
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +010051*
52*================================================================ */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070053
54#include <linux/module.h>
55#include <linux/kernel.h>
56#include <linux/sched.h>
57#include <linux/types.h>
58#include <linux/skbuff.h>
59#include <linux/slab.h>
60#include <linux/wireless.h>
61#include <linux/netdevice.h>
62#include <linux/etherdevice.h>
63#include <linux/if_ether.h>
Moritz Muehlenhoffae262302009-01-21 22:00:45 +010064#include <linux/byteorder/generic.h>
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070065
66#include <asm/byteorder.h>
67
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070068#include "p80211types.h"
69#include "p80211hdr.h"
70#include "p80211conv.h"
71#include "p80211mgmt.h"
72#include "p80211msg.h"
73#include "p80211netdev.h"
74#include "p80211ioctl.h"
75#include "p80211req.h"
76
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +010077static u8 oui_rfc1042[] = { 0x00, 0x00, 0x00 };
78static u8 oui_8021h[] = { 0x00, 0x00, 0xf8 };
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070079
80/*----------------------------------------------------------------
81* p80211pb_ether_to_80211
82*
83* Uses the contents of the ether frame and the etherconv setting
84* to build the elements of the 802.11 frame.
85*
86* We don't actually set
87* up the frame header here. That's the MAC's job. We're only handling
88* conversion of DIXII or 802.3+LLC frames to something that works
89* with 802.11.
90*
91* Note -- 802.11 header is NOT part of the skb. Likewise, the 802.11
92* FCS is also not present and will need to be added elsewhere.
93*
94* Arguments:
95* ethconv Conversion type to perform
96* skb skbuff containing the ether frame
97* p80211_hdr 802.11 header
98*
99* Returns:
100* 0 on success, non-zero otherwise
101*
102* Call context:
103* May be called in interrupt or non-interrupt context
104----------------------------------------------------------------*/
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530105int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv,
106 struct sk_buff *skb, p80211_hdr_t *p80211_hdr,
107 p80211_metawep_t *p80211_wep)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700108{
109
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100110 u16 fc;
111 u16 proto;
112 wlan_ethhdr_t e_hdr;
113 wlan_llc_t *e_llc;
114 wlan_snap_t *e_snap;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700115 int foo;
116
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700117 memcpy(&e_hdr, skb->data, sizeof(e_hdr));
118
119 if (skb->len <= 0) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100120 pr_debug("zero-length skb!\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700121 return 1;
122 }
123
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100124 if (ethconv == WLAN_ETHCONV_ENCAP) { /* simplest case */
125 pr_debug("ENCAP len: %d\n", skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700126 /* here, we don't care what kind of ether frm. Just stick it */
127 /* in the 80211 payload */
128 /* which is to say, leave the skb alone. */
129 } else {
130 /* step 1: classify ether frame, DIX or 802.3? */
131 proto = ntohs(e_hdr.type);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100132 if (proto <= 1500) {
133 pr_debug("802.3 len: %d\n", skb->len);
134 /* codes <= 1500 reserved for 802.3 lengths */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700135 /* it's 802.3, pass ether payload unchanged, */
136
137 /* trim off ethernet header */
138 skb_pull(skb, WLAN_ETHHDR_LEN);
139
140 /* leave off any PAD octets. */
141 skb_trim(skb, proto);
142 } else {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100143 pr_debug("DIXII len: %d\n", skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700144 /* it's DIXII, time for some conversion */
145
146 /* trim off ethernet header */
147 skb_pull(skb, WLAN_ETHHDR_LEN);
148
149 /* tack on SNAP */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100150 e_snap =
151 (wlan_snap_t *) skb_push(skb, sizeof(wlan_snap_t));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700152 e_snap->type = htons(proto);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100153 if (ethconv == WLAN_ETHCONV_8021h
154 && p80211_stt_findproto(proto)) {
155 memcpy(e_snap->oui, oui_8021h,
156 WLAN_IEEE_OUI_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700157 } else {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100158 memcpy(e_snap->oui, oui_rfc1042,
159 WLAN_IEEE_OUI_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700160 }
161
162 /* tack on llc */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100163 e_llc =
164 (wlan_llc_t *) skb_push(skb, sizeof(wlan_llc_t));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700165 e_llc->dsap = 0xAA; /* SNAP, see IEEE 802 */
166 e_llc->ssap = 0xAA;
167 e_llc->ctl = 0x03;
168
169 }
170 }
171
172 /* Set up the 802.11 header */
173 /* It's a data frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100174 fc = cpu_to_le16(WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
175 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700176
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100177 switch (wlandev->macmode) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700178 case WLAN_MACMODE_IBSS_STA:
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100179 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
180 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
181 memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700182 break;
183 case WLAN_MACMODE_ESS_STA:
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100184 fc |= cpu_to_le16(WLAN_SET_FC_TODS(1));
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100185 memcpy(p80211_hdr->a3.a1, wlandev->bssid, ETH_ALEN);
186 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
187 memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, ETH_ALEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700188 break;
189 case WLAN_MACMODE_ESS_AP:
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100190 fc |= cpu_to_le16(WLAN_SET_FC_FROMDS(1));
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100191 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
192 memcpy(p80211_hdr->a3.a2, wlandev->bssid, ETH_ALEN);
193 memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, ETH_ALEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700194 break;
195 default:
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100196 printk(KERN_ERR
197 "Error: Converting eth to wlan in unknown mode.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700198 return 1;
199 break;
200 }
201
202 p80211_wep->data = NULL;
203
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100204 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED)
205 && (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
206 /* XXXX need to pick keynum other than default? */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700207
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700208 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700209
Andrew Elwell3f4b4e72010-02-18 23:56:13 +0100210 foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
211 skb->len, (wlandev->hostwep &HOSTWEP_DEFAULTKEY_MASK),
212 p80211_wep->iv, p80211_wep->icv);
213 if (foo) {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100214 printk(KERN_WARNING
215 "Host en-WEP failed, dropping frame (%d).\n",
216 foo);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700217 return 2;
218 }
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100219 fc |= cpu_to_le16(WLAN_SET_FC_ISWEP(1));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700220 }
221
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100222 /* skb->nh.raw = skb->data; */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700223
224 p80211_hdr->a3.fc = fc;
225 p80211_hdr->a3.dur = 0;
226 p80211_hdr->a3.seq = 0;
227
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700228 return 0;
229}
230
231/* jkriegl: from orinoco, modified */
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530232static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
233 p80211_rxmeta_t *rxmeta)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700234{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100235 int i;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700236
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100237 /* Gather wireless spy statistics: for each packet, compare the
238 * source address with out list, and if match, get the stats... */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700239
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100240 for (i = 0; i < wlandev->spy_number; i++) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700241
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100242 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700243 memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100244 wlandev->spy_stat[i].level = rxmeta->signal;
245 wlandev->spy_stat[i].noise = rxmeta->noise;
246 wlandev->spy_stat[i].qual =
247 (rxmeta->signal >
248 rxmeta->noise) ? (rxmeta->signal -
249 rxmeta->noise) : 0;
250 wlandev->spy_stat[i].updated = 0x7;
251 }
252 }
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700253}
254
255/*----------------------------------------------------------------
256* p80211pb_80211_to_ether
257*
258* Uses the contents of a received 802.11 frame and the etherconv
259* setting to build an ether frame.
260*
261* This function extracts the src and dest address from the 802.11
262* frame to use in the construction of the eth frame.
263*
264* Arguments:
265* ethconv Conversion type to perform
266* skb Packet buffer containing the 802.11 frame
267*
268* Returns:
269* 0 on success, non-zero otherwise
270*
271* Call context:
272* May be called in interrupt or non-interrupt context
273----------------------------------------------------------------*/
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530274int skb_p80211_to_ether(wlandevice_t *wlandev, u32 ethconv,
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100275 struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700276{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100277 netdevice_t *netdev = wlandev->netdev;
278 u16 fc;
279 unsigned int payload_length;
280 unsigned int payload_offset;
281 u8 daddr[WLAN_ETHADDR_LEN];
282 u8 saddr[WLAN_ETHADDR_LEN];
283 p80211_hdr_t *w_hdr;
284 wlan_ethhdr_t *e_hdr;
285 wlan_llc_t *e_llc;
286 wlan_snap_t *e_snap;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700287
288 int foo;
289
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700290 payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
291 payload_offset = WLAN_HDR_A3_LEN;
292
293 w_hdr = (p80211_hdr_t *) skb->data;
294
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100295 /* setup some vars for convenience */
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100296 fc = le16_to_cpu(w_hdr->a3.fc);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100297 if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700298 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
299 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100300 } else if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 1)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700301 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
302 memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100303 } else if ((WLAN_GET_FC_TODS(fc) == 1) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700304 memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
305 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
306 } else {
307 payload_offset = WLAN_HDR_A4_LEN;
Roel Kluin1f9e9ce2008-12-03 00:06:39 +0100308 if (payload_length < WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100309 printk(KERN_ERR "A4 frame too short!\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700310 return 1;
311 }
Roel Kluin1f9e9ce2008-12-03 00:06:39 +0100312 payload_length -= (WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700313 memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
314 memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
315 }
316
317 /* perform de-wep if necessary.. */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100318 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && WLAN_GET_FC_ISWEP(fc)
319 && (wlandev->hostwep & HOSTWEP_DECRYPT)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700320 if (payload_length <= 8) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100321 printk(KERN_ERR "WEP frame too short (%u).\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100322 skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700323 return 1;
324 }
Andrew Elwell3f4b4e72010-02-18 23:56:13 +0100325 foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700326 payload_length - 8, -1,
327 skb->data + payload_offset,
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100328 skb->data + payload_offset +
Andrew Elwell3f4b4e72010-02-18 23:56:13 +0100329 payload_length - 4);
330 if (foo) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700331 /* de-wep failed, drop skb. */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530332 pr_debug("Host de-WEP failed, dropping frame (%d).\n",
333 foo);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700334 wlandev->rx.decrypt_err++;
335 return 2;
336 }
337
338 /* subtract the IV+ICV length off the payload */
339 payload_length -= 8;
340 /* chop off the IV */
341 skb_pull(skb, 4);
342 /* chop off the ICV. */
343 skb_trim(skb, skb->len - 4);
344
345 wlandev->rx.decrypt++;
346 }
347
348 e_hdr = (wlan_ethhdr_t *) (skb->data + payload_offset);
349
350 e_llc = (wlan_llc_t *) (skb->data + payload_offset);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100351 e_snap =
352 (wlan_snap_t *) (skb->data + payload_offset + sizeof(wlan_llc_t));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700353
354 /* Test for the various encodings */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100355 if ((payload_length >= sizeof(wlan_ethhdr_t)) &&
356 (e_llc->dsap != 0xaa || e_llc->ssap != 0xaa) &&
357 ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700358 (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100359 pr_debug("802.3 ENCAP len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700360 /* 802.3 Encapsulated */
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000361 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100362 if (payload_length > (netdev->mtu + WLAN_ETHHDR_LEN)) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000363 /* A bogus length ethfrm has been encap'd. */
364 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100365 printk(KERN_ERR "ENCAP frame too large (%d > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100366 payload_length, netdev->mtu + WLAN_ETHHDR_LEN);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000367 return 1;
368 }
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700369
370 /* Chop off the 802.11 header. it's already sane. */
371 skb_pull(skb, payload_offset);
372 /* chop off the 802.11 CRC */
373 skb_trim(skb, skb->len - WLAN_CRC_LEN);
374
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100375 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t))
376 && (e_llc->dsap == 0xaa) && (e_llc->ssap == 0xaa)
377 && (e_llc->ctl == 0x03)
378 &&
379 (((memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) == 0)
380 && (ethconv == WLAN_ETHCONV_8021h)
381 && (p80211_stt_findproto(le16_to_cpu(e_snap->type))))
382 || (memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) !=
383 0))) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100384 pr_debug("SNAP+RFC1042 len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700385 /* it's a SNAP + RFC1042 frame && protocol is in STT */
386 /* build 802.3 + RFC1042 */
387
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000388 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100389 if (payload_length > netdev->mtu) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000390 /* A bogus length ethfrm has been sent. */
391 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100392 printk(KERN_ERR "SNAP frame too large (%d > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100393 payload_length, netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000394 return 1;
395 }
396
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700397 /* chop 802.11 header from skb. */
398 skb_pull(skb, payload_offset);
399
400 /* create 802.3 header at beginning of skb. */
401 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
402 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
403 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
404 e_hdr->type = htons(payload_length);
405
406 /* chop off the 802.11 CRC */
407 skb_trim(skb, skb->len - WLAN_CRC_LEN);
408
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100409 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t))
410 && (e_llc->dsap == 0xaa) && (e_llc->ssap == 0xaa)
411 && (e_llc->ctl == 0x03)) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100412 pr_debug("802.1h/RFC1042 len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700413 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
414 /* build a DIXII + RFC894 */
415
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000416 /* Test for an overlength frame */
417 if ((payload_length - sizeof(wlan_llc_t) - sizeof(wlan_snap_t))
418 > netdev->mtu) {
419 /* A bogus length ethfrm has been sent. */
420 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100421 printk(KERN_ERR "DIXII frame too large (%ld > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100422 (long int)(payload_length - sizeof(wlan_llc_t) -
423 sizeof(wlan_snap_t)), netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000424 return 1;
425 }
426
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700427 /* chop 802.11 header from skb. */
428 skb_pull(skb, payload_offset);
429
430 /* chop llc header from skb. */
431 skb_pull(skb, sizeof(wlan_llc_t));
432
433 /* chop snap header from skb. */
434 skb_pull(skb, sizeof(wlan_snap_t));
435
436 /* create 802.3 header at beginning of skb. */
437 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
438 e_hdr->type = e_snap->type;
439 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
440 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
441
442 /* chop off the 802.11 CRC */
443 skb_trim(skb, skb->len - WLAN_CRC_LEN);
444 } else {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100445 pr_debug("NON-ENCAP len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700446 /* any NON-ENCAP */
447 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
448 /* build an 802.3 frame */
449 /* allocate space and setup hostbuf */
450
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000451 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100452 if (payload_length > netdev->mtu) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000453 /* A bogus length ethfrm has been sent. */
454 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100455 printk(KERN_ERR "OTHER frame too large (%d > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100456 payload_length, netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000457 return 1;
458 }
459
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700460 /* Chop off the 802.11 header. */
461 skb_pull(skb, payload_offset);
462
463 /* create 802.3 header at beginning of skb. */
464 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
465 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
466 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
467 e_hdr->type = htons(payload_length);
468
469 /* chop off the 802.11 CRC */
470 skb_trim(skb, skb->len - WLAN_CRC_LEN);
471
472 }
473
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100474 /*
475 * Note that eth_type_trans() expects an skb w/ skb->data pointing
476 * at the MAC header, it then sets the following skb members:
477 * skb->mac_header,
478 * skb->data, and
479 * skb->pkt_type.
480 * It then _returns_ the value that _we're_ supposed to stuff in
481 * skb->protocol. This is nuts.
482 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700483 skb->protocol = eth_type_trans(skb, netdev);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700484
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100485 /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700486 /* jkriegl: only process signal/noise if requested by iwspy */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100487 if (wlandev->spy_number)
488 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source,
489 P80211SKB_RXMETA(skb));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700490
491 /* Free the metadata */
492 p80211skb_rxmeta_detach(skb);
493
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700494 return 0;
495}
496
497/*----------------------------------------------------------------
498* p80211_stt_findproto
499*
500* Searches the 802.1h Selective Translation Table for a given
501* protocol.
502*
503* Arguments:
504* proto protocl number (in host order) to search for.
505*
506* Returns:
507* 1 - if the table is empty or a match is found.
508* 0 - if the table is non-empty and a match is not found.
509*
510* Call context:
511* May be called in interrupt or non-interrupt context
512----------------------------------------------------------------*/
Solomon Peachyaaad4302008-10-29 10:42:53 -0400513int p80211_stt_findproto(u16 proto)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700514{
515 /* Always return found for now. This is the behavior used by the */
516 /* Zoom Win95 driver when 802.1h mode is selected */
517 /* TODO: If necessary, add an actual search we'll probably
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100518 need this to match the CMAC's way of doing things.
519 Need to do some testing to confirm.
520 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700521
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100522 if (proto == 0x80f3) /* APPLETALK */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700523 return 1;
524
525 return 0;
526}
527
528/*----------------------------------------------------------------
529* p80211skb_rxmeta_detach
530*
531* Disconnects the frmmeta and rxmeta from an skb.
532*
533* Arguments:
534* wlandev The wlandev this skb belongs to.
535* skb The skb we're attaching to.
536*
537* Returns:
538* 0 on success, non-zero otherwise
539*
540* Call context:
541* May be called in interrupt or non-interrupt context
542----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100543void p80211skb_rxmeta_detach(struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700544{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100545 p80211_rxmeta_t *rxmeta;
546 p80211_frmmeta_t *frmmeta;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700547
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700548 /* Sanity checks */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100549 if (skb == NULL) { /* bad skb */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100550 pr_debug("Called w/ null skb.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700551 goto exit;
552 }
553 frmmeta = P80211SKB_FRMMETA(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100554 if (frmmeta == NULL) { /* no magic */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100555 pr_debug("Called w/ bad frmmeta magic.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700556 goto exit;
557 }
558 rxmeta = frmmeta->rx;
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100559 if (rxmeta == NULL) { /* bad meta ptr */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100560 pr_debug("Called w/ bad rxmeta ptr.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700561 goto exit;
562 }
563
564 /* Free rxmeta */
565 kfree(rxmeta);
566
567 /* Clear skb->cb */
568 memset(skb->cb, 0, sizeof(skb->cb));
569exit:
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700570 return;
571}
572
573/*----------------------------------------------------------------
574* p80211skb_rxmeta_attach
575*
576* Allocates a p80211rxmeta structure, initializes it, and attaches
577* it to an skb.
578*
579* Arguments:
580* wlandev The wlandev this skb belongs to.
581* skb The skb we're attaching to.
582*
583* Returns:
584* 0 on success, non-zero otherwise
585*
586* Call context:
587* May be called in interrupt or non-interrupt context
588----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100589int p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700590{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100591 int result = 0;
592 p80211_rxmeta_t *rxmeta;
593 p80211_frmmeta_t *frmmeta;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700594
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700595 /* If these already have metadata, we error out! */
596 if (P80211SKB_RXMETA(skb) != NULL) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100597 printk(KERN_ERR "%s: RXmeta already attached!\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100598 wlandev->name);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700599 result = 0;
600 goto exit;
601 }
602
603 /* Allocate the rxmeta */
604 rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
605
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100606 if (rxmeta == NULL) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100607 printk(KERN_ERR "%s: Failed to allocate rxmeta.\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100608 wlandev->name);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700609 result = 1;
610 goto exit;
611 }
612
613 /* Initialize the rxmeta */
614 memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
615 rxmeta->wlandev = wlandev;
616 rxmeta->hosttime = jiffies;
617
618 /* Overlay a frmmeta_t onto skb->cb */
619 memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100620 frmmeta = (p80211_frmmeta_t *) (skb->cb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700621 frmmeta->magic = P80211_FRMMETA_MAGIC;
622 frmmeta->rx = rxmeta;
623exit:
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700624 return result;
625}
626
627/*----------------------------------------------------------------
628* p80211skb_free
629*
630* Frees an entire p80211skb by checking and freeing the meta struct
631* and then freeing the skb.
632*
633* Arguments:
634* wlandev The wlandev this skb belongs to.
635* skb The skb we're attaching to.
636*
637* Returns:
638* 0 on success, non-zero otherwise
639*
640* Call context:
641* May be called in interrupt or non-interrupt context
642----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100643void p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700644{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100645 p80211_frmmeta_t *meta;
Moritz Muehlenhoff8a251b52009-01-21 22:00:44 +0100646
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700647 meta = P80211SKB_FRMMETA(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100648 if (meta && meta->rx)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700649 p80211skb_rxmeta_detach(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100650 else
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100651 printk(KERN_ERR "Freeing an skb (%p) w/ no frmmeta.\n", skb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700652 dev_kfree_skb(skb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700653 return;
654}