blob: 123cb9a2b31dec3fd9abe93fba8101101abea6c0 [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 "wlan_compat.h"
69
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070070#include "p80211types.h"
71#include "p80211hdr.h"
72#include "p80211conv.h"
73#include "p80211mgmt.h"
74#include "p80211msg.h"
75#include "p80211netdev.h"
76#include "p80211ioctl.h"
77#include "p80211req.h"
78
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +010079static u8 oui_rfc1042[] = { 0x00, 0x00, 0x00 };
80static u8 oui_8021h[] = { 0x00, 0x00, 0xf8 };
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070081
82/*----------------------------------------------------------------
83* p80211pb_ether_to_80211
84*
85* Uses the contents of the ether frame and the etherconv setting
86* to build the elements of the 802.11 frame.
87*
88* We don't actually set
89* up the frame header here. That's the MAC's job. We're only handling
90* conversion of DIXII or 802.3+LLC frames to something that works
91* with 802.11.
92*
93* Note -- 802.11 header is NOT part of the skb. Likewise, the 802.11
94* FCS is also not present and will need to be added elsewhere.
95*
96* Arguments:
97* ethconv Conversion type to perform
98* skb skbuff containing the ether frame
99* p80211_hdr 802.11 header
100*
101* Returns:
102* 0 on success, non-zero otherwise
103*
104* Call context:
105* May be called in interrupt or non-interrupt context
106----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100107int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv,
108 struct sk_buff *skb, p80211_hdr_t *p80211_hdr,
109 p80211_metawep_t *p80211_wep)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700110{
111
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100112 u16 fc;
113 u16 proto;
114 wlan_ethhdr_t e_hdr;
115 wlan_llc_t *e_llc;
116 wlan_snap_t *e_snap;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700117 int foo;
118
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700119 memcpy(&e_hdr, skb->data, sizeof(e_hdr));
120
121 if (skb->len <= 0) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100122 pr_debug("zero-length skb!\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700123 return 1;
124 }
125
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100126 if (ethconv == WLAN_ETHCONV_ENCAP) { /* simplest case */
127 pr_debug("ENCAP len: %d\n", skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700128 /* here, we don't care what kind of ether frm. Just stick it */
129 /* in the 80211 payload */
130 /* which is to say, leave the skb alone. */
131 } else {
132 /* step 1: classify ether frame, DIX or 802.3? */
133 proto = ntohs(e_hdr.type);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100134 if (proto <= 1500) {
135 pr_debug("802.3 len: %d\n", skb->len);
136 /* codes <= 1500 reserved for 802.3 lengths */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700137 /* it's 802.3, pass ether payload unchanged, */
138
139 /* trim off ethernet header */
140 skb_pull(skb, WLAN_ETHHDR_LEN);
141
142 /* leave off any PAD octets. */
143 skb_trim(skb, proto);
144 } else {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100145 pr_debug("DIXII len: %d\n", skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700146 /* it's DIXII, time for some conversion */
147
148 /* trim off ethernet header */
149 skb_pull(skb, WLAN_ETHHDR_LEN);
150
151 /* tack on SNAP */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100152 e_snap =
153 (wlan_snap_t *) skb_push(skb, sizeof(wlan_snap_t));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700154 e_snap->type = htons(proto);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100155 if (ethconv == WLAN_ETHCONV_8021h
156 && p80211_stt_findproto(proto)) {
157 memcpy(e_snap->oui, oui_8021h,
158 WLAN_IEEE_OUI_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700159 } else {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100160 memcpy(e_snap->oui, oui_rfc1042,
161 WLAN_IEEE_OUI_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700162 }
163
164 /* tack on llc */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100165 e_llc =
166 (wlan_llc_t *) skb_push(skb, sizeof(wlan_llc_t));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700167 e_llc->dsap = 0xAA; /* SNAP, see IEEE 802 */
168 e_llc->ssap = 0xAA;
169 e_llc->ctl = 0x03;
170
171 }
172 }
173
174 /* Set up the 802.11 header */
175 /* It's a data frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100176 fc = cpu_to_le16(WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
177 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700178
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100179 switch (wlandev->macmode) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700180 case WLAN_MACMODE_IBSS_STA:
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100181 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
182 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
183 memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700184 break;
185 case WLAN_MACMODE_ESS_STA:
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100186 fc |= cpu_to_le16(WLAN_SET_FC_TODS(1));
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100187 memcpy(p80211_hdr->a3.a1, wlandev->bssid, ETH_ALEN);
188 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
189 memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, ETH_ALEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700190 break;
191 case WLAN_MACMODE_ESS_AP:
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100192 fc |= cpu_to_le16(WLAN_SET_FC_FROMDS(1));
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100193 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
194 memcpy(p80211_hdr->a3.a2, wlandev->bssid, ETH_ALEN);
195 memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, ETH_ALEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700196 break;
197 default:
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100198 printk(KERN_ERR
199 "Error: Converting eth to wlan in unknown mode.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700200 return 1;
201 break;
202 }
203
204 p80211_wep->data = NULL;
205
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100206 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED)
207 && (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
208 /* XXXX need to pick keynum other than default? */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700209
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700210 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700211
212 if ((foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
213 skb->len,
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100214 (wlandev->
215 hostwep & HOSTWEP_DEFAULTKEY_MASK),
216 p80211_wep->iv, p80211_wep->icv))) {
217 printk(KERN_WARNING
218 "Host en-WEP failed, dropping frame (%d).\n",
219 foo);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700220 return 2;
221 }
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100222 fc |= cpu_to_le16(WLAN_SET_FC_ISWEP(1));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700223 }
224
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100225 /* skb->nh.raw = skb->data; */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700226
227 p80211_hdr->a3.fc = fc;
228 p80211_hdr->a3.dur = 0;
229 p80211_hdr->a3.seq = 0;
230
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700231 return 0;
232}
233
234/* jkriegl: from orinoco, modified */
235static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
236 p80211_rxmeta_t *rxmeta)
237{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100238 int i;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700239
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100240 /* Gather wireless spy statistics: for each packet, compare the
241 * source address with out list, and if match, get the stats... */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700242
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100243 for (i = 0; i < wlandev->spy_number; i++) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700244
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100245 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700246 memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100247 wlandev->spy_stat[i].level = rxmeta->signal;
248 wlandev->spy_stat[i].noise = rxmeta->noise;
249 wlandev->spy_stat[i].qual =
250 (rxmeta->signal >
251 rxmeta->noise) ? (rxmeta->signal -
252 rxmeta->noise) : 0;
253 wlandev->spy_stat[i].updated = 0x7;
254 }
255 }
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700256}
257
258/*----------------------------------------------------------------
259* p80211pb_80211_to_ether
260*
261* Uses the contents of a received 802.11 frame and the etherconv
262* setting to build an ether frame.
263*
264* This function extracts the src and dest address from the 802.11
265* frame to use in the construction of the eth frame.
266*
267* Arguments:
268* ethconv Conversion type to perform
269* skb Packet buffer containing the 802.11 frame
270*
271* Returns:
272* 0 on success, non-zero otherwise
273*
274* Call context:
275* May be called in interrupt or non-interrupt context
276----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100277int skb_p80211_to_ether(wlandevice_t *wlandev, u32 ethconv,
278 struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700279{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100280 netdevice_t *netdev = wlandev->netdev;
281 u16 fc;
282 unsigned int payload_length;
283 unsigned int payload_offset;
284 u8 daddr[WLAN_ETHADDR_LEN];
285 u8 saddr[WLAN_ETHADDR_LEN];
286 p80211_hdr_t *w_hdr;
287 wlan_ethhdr_t *e_hdr;
288 wlan_llc_t *e_llc;
289 wlan_snap_t *e_snap;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700290
291 int foo;
292
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700293 payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
294 payload_offset = WLAN_HDR_A3_LEN;
295
296 w_hdr = (p80211_hdr_t *) skb->data;
297
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100298 /* setup some vars for convenience */
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100299 fc = le16_to_cpu(w_hdr->a3.fc);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100300 if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700301 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
302 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100303 } else if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 1)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700304 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
305 memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100306 } else if ((WLAN_GET_FC_TODS(fc) == 1) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700307 memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
308 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
309 } else {
310 payload_offset = WLAN_HDR_A4_LEN;
Roel Kluin1f9e9ce2008-12-03 00:06:39 +0100311 if (payload_length < WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100312 printk(KERN_ERR "A4 frame too short!\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700313 return 1;
314 }
Roel Kluin1f9e9ce2008-12-03 00:06:39 +0100315 payload_length -= (WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700316 memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
317 memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
318 }
319
320 /* perform de-wep if necessary.. */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100321 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && WLAN_GET_FC_ISWEP(fc)
322 && (wlandev->hostwep & HOSTWEP_DECRYPT)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700323 if (payload_length <= 8) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100324 printk(KERN_ERR "WEP frame too short (%u).\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100325 skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700326 return 1;
327 }
328 if ((foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
329 payload_length - 8, -1,
330 skb->data + payload_offset,
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100331 skb->data + payload_offset +
332 payload_length - 4))) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700333 /* de-wep failed, drop skb. */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100334 printk(KERN_DEBUG
335 "Host de-WEP failed, dropping frame (%d).\n",
336 foo);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700337 wlandev->rx.decrypt_err++;
338 return 2;
339 }
340
341 /* subtract the IV+ICV length off the payload */
342 payload_length -= 8;
343 /* chop off the IV */
344 skb_pull(skb, 4);
345 /* chop off the ICV. */
346 skb_trim(skb, skb->len - 4);
347
348 wlandev->rx.decrypt++;
349 }
350
351 e_hdr = (wlan_ethhdr_t *) (skb->data + payload_offset);
352
353 e_llc = (wlan_llc_t *) (skb->data + payload_offset);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100354 e_snap =
355 (wlan_snap_t *) (skb->data + payload_offset + sizeof(wlan_llc_t));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700356
357 /* Test for the various encodings */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100358 if ((payload_length >= sizeof(wlan_ethhdr_t)) &&
359 (e_llc->dsap != 0xaa || e_llc->ssap != 0xaa) &&
360 ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700361 (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100362 pr_debug("802.3 ENCAP len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700363 /* 802.3 Encapsulated */
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000364 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100365 if (payload_length > (netdev->mtu + WLAN_ETHHDR_LEN)) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000366 /* A bogus length ethfrm has been encap'd. */
367 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100368 printk(KERN_ERR "ENCAP frame too large (%d > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100369 payload_length, netdev->mtu + WLAN_ETHHDR_LEN);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000370 return 1;
371 }
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700372
373 /* Chop off the 802.11 header. it's already sane. */
374 skb_pull(skb, payload_offset);
375 /* chop off the 802.11 CRC */
376 skb_trim(skb, skb->len - WLAN_CRC_LEN);
377
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100378 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t))
379 && (e_llc->dsap == 0xaa) && (e_llc->ssap == 0xaa)
380 && (e_llc->ctl == 0x03)
381 &&
382 (((memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) == 0)
383 && (ethconv == WLAN_ETHCONV_8021h)
384 && (p80211_stt_findproto(le16_to_cpu(e_snap->type))))
385 || (memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) !=
386 0))) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100387 pr_debug("SNAP+RFC1042 len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700388 /* it's a SNAP + RFC1042 frame && protocol is in STT */
389 /* build 802.3 + RFC1042 */
390
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000391 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100392 if (payload_length > netdev->mtu) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000393 /* A bogus length ethfrm has been sent. */
394 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100395 printk(KERN_ERR "SNAP frame too large (%d > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100396 payload_length, netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000397 return 1;
398 }
399
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700400 /* chop 802.11 header from skb. */
401 skb_pull(skb, payload_offset);
402
403 /* create 802.3 header at beginning of skb. */
404 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
405 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
406 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
407 e_hdr->type = htons(payload_length);
408
409 /* chop off the 802.11 CRC */
410 skb_trim(skb, skb->len - WLAN_CRC_LEN);
411
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100412 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t))
413 && (e_llc->dsap == 0xaa) && (e_llc->ssap == 0xaa)
414 && (e_llc->ctl == 0x03)) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100415 pr_debug("802.1h/RFC1042 len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700416 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
417 /* build a DIXII + RFC894 */
418
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000419 /* Test for an overlength frame */
420 if ((payload_length - sizeof(wlan_llc_t) - sizeof(wlan_snap_t))
421 > netdev->mtu) {
422 /* A bogus length ethfrm has been sent. */
423 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100424 printk(KERN_ERR "DIXII frame too large (%ld > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100425 (long int)(payload_length - sizeof(wlan_llc_t) -
426 sizeof(wlan_snap_t)), netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000427 return 1;
428 }
429
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700430 /* chop 802.11 header from skb. */
431 skb_pull(skb, payload_offset);
432
433 /* chop llc header from skb. */
434 skb_pull(skb, sizeof(wlan_llc_t));
435
436 /* chop snap header from skb. */
437 skb_pull(skb, sizeof(wlan_snap_t));
438
439 /* create 802.3 header at beginning of skb. */
440 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
441 e_hdr->type = e_snap->type;
442 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
443 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
444
445 /* chop off the 802.11 CRC */
446 skb_trim(skb, skb->len - WLAN_CRC_LEN);
447 } else {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100448 pr_debug("NON-ENCAP len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700449 /* any NON-ENCAP */
450 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
451 /* build an 802.3 frame */
452 /* allocate space and setup hostbuf */
453
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000454 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100455 if (payload_length > netdev->mtu) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000456 /* A bogus length ethfrm has been sent. */
457 /* Is someone trying an oflow attack? */
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100458 printk(KERN_ERR "OTHER frame too large (%d > %d)\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100459 payload_length, netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000460 return 1;
461 }
462
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700463 /* Chop off the 802.11 header. */
464 skb_pull(skb, payload_offset);
465
466 /* create 802.3 header at beginning of skb. */
467 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
468 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
469 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
470 e_hdr->type = htons(payload_length);
471
472 /* chop off the 802.11 CRC */
473 skb_trim(skb, skb->len - WLAN_CRC_LEN);
474
475 }
476
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100477 /*
478 * Note that eth_type_trans() expects an skb w/ skb->data pointing
479 * at the MAC header, it then sets the following skb members:
480 * skb->mac_header,
481 * skb->data, and
482 * skb->pkt_type.
483 * It then _returns_ the value that _we're_ supposed to stuff in
484 * skb->protocol. This is nuts.
485 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700486 skb->protocol = eth_type_trans(skb, netdev);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700487
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100488 /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700489 /* jkriegl: only process signal/noise if requested by iwspy */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100490 if (wlandev->spy_number)
491 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source,
492 P80211SKB_RXMETA(skb));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700493
494 /* Free the metadata */
495 p80211skb_rxmeta_detach(skb);
496
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700497 return 0;
498}
499
500/*----------------------------------------------------------------
501* p80211_stt_findproto
502*
503* Searches the 802.1h Selective Translation Table for a given
504* protocol.
505*
506* Arguments:
507* proto protocl number (in host order) to search for.
508*
509* Returns:
510* 1 - if the table is empty or a match is found.
511* 0 - if the table is non-empty and a match is not found.
512*
513* Call context:
514* May be called in interrupt or non-interrupt context
515----------------------------------------------------------------*/
Solomon Peachyaaad4302008-10-29 10:42:53 -0400516int p80211_stt_findproto(u16 proto)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700517{
518 /* Always return found for now. This is the behavior used by the */
519 /* Zoom Win95 driver when 802.1h mode is selected */
520 /* TODO: If necessary, add an actual search we'll probably
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100521 need this to match the CMAC's way of doing things.
522 Need to do some testing to confirm.
523 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700524
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100525 if (proto == 0x80f3) /* APPLETALK */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700526 return 1;
527
528 return 0;
529}
530
531/*----------------------------------------------------------------
532* p80211skb_rxmeta_detach
533*
534* Disconnects the frmmeta and rxmeta from an skb.
535*
536* Arguments:
537* wlandev The wlandev this skb belongs to.
538* skb The skb we're attaching to.
539*
540* Returns:
541* 0 on success, non-zero otherwise
542*
543* Call context:
544* May be called in interrupt or non-interrupt context
545----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100546void p80211skb_rxmeta_detach(struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700547{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100548 p80211_rxmeta_t *rxmeta;
549 p80211_frmmeta_t *frmmeta;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700550
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700551 /* Sanity checks */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100552 if (skb == NULL) { /* bad skb */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100553 pr_debug("Called w/ null skb.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700554 goto exit;
555 }
556 frmmeta = P80211SKB_FRMMETA(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100557 if (frmmeta == NULL) { /* no magic */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100558 pr_debug("Called w/ bad frmmeta magic.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700559 goto exit;
560 }
561 rxmeta = frmmeta->rx;
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100562 if (rxmeta == NULL) { /* bad meta ptr */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100563 pr_debug("Called w/ bad rxmeta ptr.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700564 goto exit;
565 }
566
567 /* Free rxmeta */
568 kfree(rxmeta);
569
570 /* Clear skb->cb */
571 memset(skb->cb, 0, sizeof(skb->cb));
572exit:
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700573 return;
574}
575
576/*----------------------------------------------------------------
577* p80211skb_rxmeta_attach
578*
579* Allocates a p80211rxmeta structure, initializes it, and attaches
580* it to an skb.
581*
582* Arguments:
583* wlandev The wlandev this skb belongs to.
584* skb The skb we're attaching to.
585*
586* Returns:
587* 0 on success, non-zero otherwise
588*
589* Call context:
590* May be called in interrupt or non-interrupt context
591----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100592int p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700593{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100594 int result = 0;
595 p80211_rxmeta_t *rxmeta;
596 p80211_frmmeta_t *frmmeta;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700597
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700598 /* If these already have metadata, we error out! */
599 if (P80211SKB_RXMETA(skb) != NULL) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100600 printk(KERN_ERR "%s: RXmeta already attached!\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100601 wlandev->name);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700602 result = 0;
603 goto exit;
604 }
605
606 /* Allocate the rxmeta */
607 rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
608
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100609 if (rxmeta == NULL) {
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100610 printk(KERN_ERR "%s: Failed to allocate rxmeta.\n",
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100611 wlandev->name);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700612 result = 1;
613 goto exit;
614 }
615
616 /* Initialize the rxmeta */
617 memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
618 rxmeta->wlandev = wlandev;
619 rxmeta->hosttime = jiffies;
620
621 /* Overlay a frmmeta_t onto skb->cb */
622 memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100623 frmmeta = (p80211_frmmeta_t *) (skb->cb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700624 frmmeta->magic = P80211_FRMMETA_MAGIC;
625 frmmeta->rx = rxmeta;
626exit:
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700627 return result;
628}
629
630/*----------------------------------------------------------------
631* p80211skb_free
632*
633* Frees an entire p80211skb by checking and freeing the meta struct
634* and then freeing the skb.
635*
636* Arguments:
637* wlandev The wlandev this skb belongs to.
638* skb The skb we're attaching to.
639*
640* Returns:
641* 0 on success, non-zero otherwise
642*
643* Call context:
644* May be called in interrupt or non-interrupt context
645----------------------------------------------------------------*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100646void p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700647{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100648 p80211_frmmeta_t *meta;
Moritz Muehlenhoff8a251b52009-01-21 22:00:44 +0100649
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700650 meta = P80211SKB_FRMMETA(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100651 if (meta && meta->rx)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700652 p80211skb_rxmeta_detach(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100653 else
Moritz Muehlenhoffedbd6062009-01-25 21:55:00 +0100654 printk(KERN_ERR "Freeing an skb (%p) w/ no frmmeta.\n", skb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700655 dev_kfree_skb(skb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700656 return;
657}