]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/wlan-ng/p80211conv.c
Staging: add wlan-ng prism2 usb driver
[linux-2.6-omap-h63xx.git] / drivers / staging / wlan-ng / p80211conv.c
1 /* 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 * --------------------------------------------------------------------
51 */
52 /*================================================================*/
53 /* System Includes */
54
55 #define __NO_VERSION__          /* prevent the static definition */
56
57
58 #include <linux/version.h>
59
60 #include <linux/module.h>
61 #include <linux/kernel.h>
62 #include <linux/sched.h>
63 #include <linux/types.h>
64 #include <linux/skbuff.h>
65 #include <linux/slab.h>
66 #include <linux/wireless.h>
67 #include <linux/netdevice.h>
68 #include <linux/etherdevice.h>
69 #include <linux/if_ether.h>
70
71 #include <asm/byteorder.h>
72
73 #include "version.h"
74 #include "wlan_compat.h"
75
76 /*================================================================*/
77 /* Project Includes */
78
79 #include "p80211types.h"
80 #include "p80211hdr.h"
81 #include "p80211conv.h"
82 #include "p80211mgmt.h"
83 #include "p80211msg.h"
84 #include "p80211netdev.h"
85 #include "p80211ioctl.h"
86 #include "p80211req.h"
87
88
89 /*================================================================*/
90 /* Local Constants */
91
92 /*================================================================*/
93 /* Local Macros */
94
95
96 /*================================================================*/
97 /* Local Types */
98
99
100 /*================================================================*/
101 /* Local Static Definitions */
102
103 static UINT8    oui_rfc1042[] = {0x00, 0x00, 0x00};
104 static UINT8    oui_8021h[] = {0x00, 0x00, 0xf8};
105
106 /*================================================================*/
107 /* Local Function Declarations */
108
109
110 /*================================================================*/
111 /* Function Definitions */
112
113 /*----------------------------------------------------------------
114 * p80211pb_ether_to_80211
115 *
116 * Uses the contents of the ether frame and the etherconv setting
117 * to build the elements of the 802.11 frame.
118 *
119 * We don't actually set
120 * up the frame header here.  That's the MAC's job.  We're only handling
121 * conversion of DIXII or 802.3+LLC frames to something that works
122 * with 802.11.
123 *
124 * Note -- 802.11 header is NOT part of the skb.  Likewise, the 802.11
125 *         FCS is also not present and will need to be added elsewhere.
126 *
127 * Arguments:
128 *       ethconv         Conversion type to perform
129 *       skb             skbuff containing the ether frame
130 *       p80211_hdr      802.11 header
131 *
132 * Returns:
133 *       0 on success, non-zero otherwise
134 *
135 * Call context:
136 *       May be called in interrupt or non-interrupt context
137 ----------------------------------------------------------------*/
138 int skb_ether_to_p80211( wlandevice_t *wlandev, UINT32 ethconv, struct sk_buff *skb, p80211_hdr_t *p80211_hdr, p80211_metawep_t *p80211_wep)
139 {
140
141         UINT16          fc;
142         UINT16          proto;
143         wlan_ethhdr_t   e_hdr;
144         wlan_llc_t      *e_llc;
145         wlan_snap_t     *e_snap;
146         int foo;
147
148         DBFENTER;
149         memcpy(&e_hdr, skb->data, sizeof(e_hdr));
150
151         if (skb->len <= 0) {
152                 WLAN_LOG_DEBUG(1, "zero-length skb!\n");
153                 return 1;
154         }
155
156         if ( ethconv == WLAN_ETHCONV_ENCAP ) { /* simplest case */
157                 WLAN_LOG_DEBUG(3, "ENCAP len: %d\n", skb->len);
158                 /* here, we don't care what kind of ether frm. Just stick it */
159                 /*  in the 80211 payload */
160                 /* which is to say, leave the skb alone. */
161         } else {
162                 /* step 1: classify ether frame, DIX or 802.3? */
163                 proto = ntohs(e_hdr.type);
164                 if ( proto <= 1500 ) {
165                         WLAN_LOG_DEBUG(3, "802.3 len: %d\n", skb->len);
166                         /* codes <= 1500 reserved for 802.3 lengths */
167                         /* it's 802.3, pass ether payload unchanged,  */
168
169                         /* trim off ethernet header */
170                         skb_pull(skb, WLAN_ETHHDR_LEN);
171
172                         /*   leave off any PAD octets.  */
173                         skb_trim(skb, proto);
174                 } else {
175                         WLAN_LOG_DEBUG(3, "DIXII len: %d\n", skb->len);
176                         /* it's DIXII, time for some conversion */
177
178                         /* trim off ethernet header */
179                         skb_pull(skb, WLAN_ETHHDR_LEN);
180
181                         /* tack on SNAP */
182                         e_snap = (wlan_snap_t *) skb_push(skb, sizeof(wlan_snap_t));
183                         e_snap->type = htons(proto);
184                         if ( ethconv == WLAN_ETHCONV_8021h && p80211_stt_findproto(proto) ) {
185                                 memcpy( e_snap->oui, oui_8021h, WLAN_IEEE_OUI_LEN);
186                         } else {
187                                 memcpy( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN);
188                         }
189
190                         /* tack on llc */
191                         e_llc = (wlan_llc_t *) skb_push(skb, sizeof(wlan_llc_t));
192                         e_llc->dsap = 0xAA;     /* SNAP, see IEEE 802 */
193                         e_llc->ssap = 0xAA;
194                         e_llc->ctl = 0x03;
195
196                 }
197         }
198
199         /* Set up the 802.11 header */
200         /* It's a data frame */
201         fc = host2ieee16( WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
202                           WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
203
204         switch ( wlandev->macmode ) {
205         case WLAN_MACMODE_IBSS_STA:
206                 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, WLAN_ADDR_LEN);
207                 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, WLAN_ADDR_LEN);
208                 memcpy(p80211_hdr->a3.a3, wlandev->bssid, WLAN_ADDR_LEN);
209                 break;
210         case WLAN_MACMODE_ESS_STA:
211                 fc |= host2ieee16(WLAN_SET_FC_TODS(1));
212                 memcpy(p80211_hdr->a3.a1, wlandev->bssid, WLAN_ADDR_LEN);
213                 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, WLAN_ADDR_LEN);
214                 memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, WLAN_ADDR_LEN);
215                 break;
216         case WLAN_MACMODE_ESS_AP:
217                 fc |= host2ieee16(WLAN_SET_FC_FROMDS(1));
218                 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, WLAN_ADDR_LEN);
219                 memcpy(p80211_hdr->a3.a2, wlandev->bssid, WLAN_ADDR_LEN);
220                 memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, WLAN_ADDR_LEN);
221                 break;
222         default:
223                 WLAN_LOG_ERROR("Error: Converting eth to wlan in unknown mode.\n");
224                 return 1;
225                 break;
226         }
227
228         p80211_wep->data = NULL;
229
230         if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
231                 // XXXX need to pick keynum other than default?
232
233 #if 1
234                 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
235 #else
236                 p80211_wep->data = skb->data;
237 #endif
238
239                 if ((foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
240                                        skb->len,
241                                 (wlandev->hostwep & HOSTWEP_DEFAULTKEY_MASK),
242                                 p80211_wep->iv, p80211_wep->icv))) {
243                         WLAN_LOG_WARNING("Host en-WEP failed, dropping frame (%d).\n", foo);
244                         return 2;
245                 }
246                 fc |= host2ieee16(WLAN_SET_FC_ISWEP(1));
247         }
248
249
250         //      skb->nh.raw = skb->data;
251
252         p80211_hdr->a3.fc = fc;
253         p80211_hdr->a3.dur = 0;
254         p80211_hdr->a3.seq = 0;
255
256         DBFEXIT;
257         return 0;
258 }
259
260 /* jkriegl: from orinoco, modified */
261 static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
262                                p80211_rxmeta_t *rxmeta)
263 {
264         int i;
265
266         /* Gather wireless spy statistics: for each packet, compare the
267          * source address with out list, and if match, get the stats... */
268
269         for (i = 0; i < wlandev->spy_number; i++) {
270
271                 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
272                         memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
273                         wlandev->spy_stat[i].level = rxmeta->signal;
274                         wlandev->spy_stat[i].noise = rxmeta->noise;
275                         wlandev->spy_stat[i].qual = (rxmeta->signal > rxmeta->noise) ? \
276                                                      (rxmeta->signal - rxmeta->noise) : 0;
277                         wlandev->spy_stat[i].updated = 0x7;
278                 }
279         }
280 }
281
282 /*----------------------------------------------------------------
283 * p80211pb_80211_to_ether
284 *
285 * Uses the contents of a received 802.11 frame and the etherconv
286 * setting to build an ether frame.
287 *
288 * This function extracts the src and dest address from the 802.11
289 * frame to use in the construction of the eth frame.
290 *
291 * Arguments:
292 *       ethconv         Conversion type to perform
293 *       skb             Packet buffer containing the 802.11 frame
294 *
295 * Returns:
296 *       0 on success, non-zero otherwise
297 *
298 * Call context:
299 *       May be called in interrupt or non-interrupt context
300 ----------------------------------------------------------------*/
301 int skb_p80211_to_ether( wlandevice_t *wlandev, UINT32 ethconv, struct sk_buff *skb)
302 {
303         netdevice_t     *netdev = wlandev->netdev;
304         UINT16          fc;
305         UINT            payload_length;
306         UINT            payload_offset;
307         UINT8           daddr[WLAN_ETHADDR_LEN];
308         UINT8           saddr[WLAN_ETHADDR_LEN];
309         p80211_hdr_t    *w_hdr;
310         wlan_ethhdr_t   *e_hdr;
311         wlan_llc_t      *e_llc;
312         wlan_snap_t     *e_snap;
313
314         int foo;
315
316         DBFENTER;
317
318         payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
319         payload_offset = WLAN_HDR_A3_LEN;
320
321         w_hdr = (p80211_hdr_t *) skb->data;
322
323         /* setup some vars for convenience */
324         fc = ieee2host16(w_hdr->a3.fc);
325         if ( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
326                 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
327                 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
328         } else if( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 1) ) {
329                 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
330                 memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
331         } else if( (WLAN_GET_FC_TODS(fc) == 1) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
332                 memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
333                 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
334         } else {
335                 payload_offset = WLAN_HDR_A4_LEN;
336                 payload_length -= ( WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN );
337                 if (payload_length < 0 ) {
338                         WLAN_LOG_ERROR("A4 frame too short!\n");
339                         return 1;
340                 }
341                 memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
342                 memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
343         }
344
345         /* perform de-wep if necessary.. */
346         if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && WLAN_GET_FC_ISWEP(fc) && (wlandev->hostwep & HOSTWEP_DECRYPT)) {
347                 if (payload_length <= 8) {
348                         WLAN_LOG_ERROR("WEP frame too short (%u).\n",
349                                         skb->len);
350                         return 1;
351                 }
352                 if ((foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
353                                        payload_length - 8, -1,
354                                        skb->data + payload_offset,
355                                        skb->data + payload_offset + payload_length - 4))) {
356                         /* de-wep failed, drop skb. */
357                         WLAN_LOG_DEBUG(1, "Host de-WEP failed, dropping frame (%d).\n", foo);
358                         wlandev->rx.decrypt_err++;
359                         return 2;
360                 }
361
362                 /* subtract the IV+ICV length off the payload */
363                 payload_length -= 8;
364                 /* chop off the IV */
365                 skb_pull(skb, 4);
366                 /* chop off the ICV. */
367                 skb_trim(skb, skb->len - 4);
368
369                 wlandev->rx.decrypt++;
370         }
371
372         e_hdr = (wlan_ethhdr_t *) (skb->data + payload_offset);
373
374         e_llc = (wlan_llc_t *) (skb->data + payload_offset);
375         e_snap = (wlan_snap_t *) (skb->data + payload_offset + sizeof(wlan_llc_t));
376
377         /* Test for the various encodings */
378         if ( (payload_length >= sizeof(wlan_ethhdr_t)) &&
379              ( e_llc->dsap != 0xaa || e_llc->ssap != 0xaa ) &&
380              ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
381              (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
382                 WLAN_LOG_DEBUG(3, "802.3 ENCAP len: %d\n", payload_length);
383                 /* 802.3 Encapsulated */
384                 /* Test for an overlength frame */
385                 if ( payload_length > (netdev->mtu + WLAN_ETHHDR_LEN)) {
386                         /* A bogus length ethfrm has been encap'd. */
387                         /* Is someone trying an oflow attack? */
388                         WLAN_LOG_ERROR("ENCAP frame too large (%d > %d)\n",
389                                 payload_length, netdev->mtu + WLAN_ETHHDR_LEN);
390                         return 1;
391                 }
392
393                 /* Chop off the 802.11 header.  it's already sane. */
394                 skb_pull(skb, payload_offset);
395                 /* chop off the 802.11 CRC */
396                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
397
398         } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
399                    (e_llc->dsap == 0xaa) &&
400                    (e_llc->ssap == 0xaa) &&
401                    (e_llc->ctl == 0x03) &&
402                    (((memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)==0) &&
403                     (ethconv == WLAN_ETHCONV_8021h) &&
404                     (p80211_stt_findproto(ieee2host16(e_snap->type)))) ||
405                     (memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)!=0)))
406         {
407                 WLAN_LOG_DEBUG(3, "SNAP+RFC1042 len: %d\n", payload_length);
408                 /* it's a SNAP + RFC1042 frame && protocol is in STT */
409                 /* build 802.3 + RFC1042 */
410
411                 /* Test for an overlength frame */
412                 if ( payload_length > netdev->mtu ) {
413                         /* A bogus length ethfrm has been sent. */
414                         /* Is someone trying an oflow attack? */
415                         WLAN_LOG_ERROR("SNAP frame too large (%d > %d)\n",
416                                 payload_length, netdev->mtu);
417                         return 1;
418                 }
419
420                 /* chop 802.11 header from skb. */
421                 skb_pull(skb, payload_offset);
422
423                 /* create 802.3 header at beginning of skb. */
424                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
425                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
426                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
427                 e_hdr->type = htons(payload_length);
428
429                 /* chop off the 802.11 CRC */
430                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
431
432         }  else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
433                     (e_llc->dsap == 0xaa) &&
434                     (e_llc->ssap == 0xaa) &&
435                     (e_llc->ctl == 0x03) ) {
436                 WLAN_LOG_DEBUG(3, "802.1h/RFC1042 len: %d\n", payload_length);
437                 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
438                 /* build a DIXII + RFC894 */
439
440                 /* Test for an overlength frame */
441                 if ((payload_length - sizeof(wlan_llc_t) - sizeof(wlan_snap_t))
442                     > netdev->mtu) {
443                         /* A bogus length ethfrm has been sent. */
444                         /* Is someone trying an oflow attack? */
445                         WLAN_LOG_ERROR("DIXII frame too large (%ld > %d)\n",
446                                         (long int) (payload_length - sizeof(wlan_llc_t) -
447                                                     sizeof(wlan_snap_t)),
448                                         netdev->mtu);
449                         return 1;
450                 }
451
452                 /* chop 802.11 header from skb. */
453                 skb_pull(skb, payload_offset);
454
455                 /* chop llc header from skb. */
456                 skb_pull(skb, sizeof(wlan_llc_t));
457
458                 /* chop snap header from skb. */
459                 skb_pull(skb, sizeof(wlan_snap_t));
460
461                 /* create 802.3 header at beginning of skb. */
462                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
463                 e_hdr->type = e_snap->type;
464                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
465                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
466
467                 /* chop off the 802.11 CRC */
468                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
469         } else {
470                 WLAN_LOG_DEBUG(3, "NON-ENCAP len: %d\n", payload_length);
471                 /* any NON-ENCAP */
472                 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
473                 /*  build an 802.3 frame */
474                 /* allocate space and setup hostbuf */
475
476                 /* Test for an overlength frame */
477                 if ( payload_length > netdev->mtu ) {
478                         /* A bogus length ethfrm has been sent. */
479                         /* Is someone trying an oflow attack? */
480                         WLAN_LOG_ERROR("OTHER frame too large (%d > %d)\n",
481                                 payload_length,
482                                 netdev->mtu);
483                         return 1;
484                 }
485
486                 /* Chop off the 802.11 header. */
487                 skb_pull(skb, payload_offset);
488
489                 /* create 802.3 header at beginning of skb. */
490                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
491                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
492                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
493                 e_hdr->type = htons(payload_length);
494
495                 /* chop off the 802.11 CRC */
496                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
497
498         }
499
500         skb->protocol = eth_type_trans(skb, netdev);
501         skb_reset_mac_header(skb);
502
503         /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
504         /* jkriegl: only process signal/noise if requested by iwspy */
505         if (wlandev->spy_number)
506                 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source, P80211SKB_RXMETA(skb));
507
508         /* Free the metadata */
509         p80211skb_rxmeta_detach(skb);
510
511         DBFEXIT;
512         return 0;
513 }
514
515 /*----------------------------------------------------------------
516 * p80211_stt_findproto
517 *
518 * Searches the 802.1h Selective Translation Table for a given
519 * protocol.
520 *
521 * Arguments:
522 *       proto   protocl number (in host order) to search for.
523 *
524 * Returns:
525 *       1 - if the table is empty or a match is found.
526 *       0 - if the table is non-empty and a match is not found.
527 *
528 * Call context:
529 *       May be called in interrupt or non-interrupt context
530 ----------------------------------------------------------------*/
531 int p80211_stt_findproto(UINT16 proto)
532 {
533         /* Always return found for now.  This is the behavior used by the */
534         /*  Zoom Win95 driver when 802.1h mode is selected */
535         /* TODO: If necessary, add an actual search we'll probably
536                  need this to match the CMAC's way of doing things.
537                  Need to do some testing to confirm.
538         */
539
540         if (proto == 0x80f3)  /* APPLETALK */
541                 return 1;
542
543         return 0;
544 }
545
546 /*----------------------------------------------------------------
547 * p80211skb_rxmeta_detach
548 *
549 * Disconnects the frmmeta and rxmeta from an skb.
550 *
551 * Arguments:
552 *       wlandev         The wlandev this skb belongs to.
553 *       skb             The skb we're attaching to.
554 *
555 * Returns:
556 *       0 on success, non-zero otherwise
557 *
558 * Call context:
559 *       May be called in interrupt or non-interrupt context
560 ----------------------------------------------------------------*/
561 void
562 p80211skb_rxmeta_detach(struct sk_buff *skb)
563 {
564         p80211_rxmeta_t         *rxmeta;
565         p80211_frmmeta_t        *frmmeta;
566
567         DBFENTER;
568         /* Sanity checks */
569         if ( skb==NULL ) {                      /* bad skb */
570                 WLAN_LOG_DEBUG(1, "Called w/ null skb.\n");
571                 goto exit;
572         }
573         frmmeta = P80211SKB_FRMMETA(skb);
574         if ( frmmeta == NULL ) {                /* no magic */
575                 WLAN_LOG_DEBUG(1, "Called w/ bad frmmeta magic.\n");
576                 goto exit;
577         }
578         rxmeta = frmmeta->rx;
579         if ( rxmeta == NULL ) {                 /* bad meta ptr */
580                 WLAN_LOG_DEBUG(1, "Called w/ bad rxmeta ptr.\n");
581                 goto exit;
582         }
583
584         /* Free rxmeta */
585         kfree(rxmeta);
586
587         /* Clear skb->cb */
588         memset(skb->cb, 0, sizeof(skb->cb));
589 exit:
590         DBFEXIT;
591         return;
592 }
593
594 /*----------------------------------------------------------------
595 * p80211skb_rxmeta_attach
596 *
597 * Allocates a p80211rxmeta structure, initializes it, and attaches
598 * it to an skb.
599 *
600 * Arguments:
601 *       wlandev         The wlandev this skb belongs to.
602 *       skb             The skb we're attaching to.
603 *
604 * Returns:
605 *       0 on success, non-zero otherwise
606 *
607 * Call context:
608 *       May be called in interrupt or non-interrupt context
609 ----------------------------------------------------------------*/
610 int
611 p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
612 {
613         int                     result = 0;
614         p80211_rxmeta_t         *rxmeta;
615         p80211_frmmeta_t        *frmmeta;
616
617         DBFENTER;
618
619         /* If these already have metadata, we error out! */
620         if (P80211SKB_RXMETA(skb) != NULL) {
621                 WLAN_LOG_ERROR("%s: RXmeta already attached!\n",
622                                 wlandev->name);
623                 result = 0;
624                 goto exit;
625         }
626
627         /* Allocate the rxmeta */
628         rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
629
630         if ( rxmeta == NULL ) {
631                 WLAN_LOG_ERROR("%s: Failed to allocate rxmeta.\n",
632                                 wlandev->name);
633                 result = 1;
634                 goto exit;
635         }
636
637         /* Initialize the rxmeta */
638         memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
639         rxmeta->wlandev = wlandev;
640         rxmeta->hosttime = jiffies;
641
642         /* Overlay a frmmeta_t onto skb->cb */
643         memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
644         frmmeta = (p80211_frmmeta_t*)(skb->cb);
645         frmmeta->magic = P80211_FRMMETA_MAGIC;
646         frmmeta->rx = rxmeta;
647 exit:
648         DBFEXIT;
649         return result;
650 }
651
652 /*----------------------------------------------------------------
653 * p80211skb_free
654 *
655 * Frees an entire p80211skb by checking and freeing the meta struct
656 * and then freeing the skb.
657 *
658 * Arguments:
659 *       wlandev         The wlandev this skb belongs to.
660 *       skb             The skb we're attaching to.
661 *
662 * Returns:
663 *       0 on success, non-zero otherwise
664 *
665 * Call context:
666 *       May be called in interrupt or non-interrupt context
667 ----------------------------------------------------------------*/
668 void
669 p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
670 {
671         p80211_frmmeta_t        *meta;
672         DBFENTER;
673         meta = P80211SKB_FRMMETA(skb);
674         if ( meta && meta->rx) {
675                 p80211skb_rxmeta_detach(skb);
676         } else {
677                 WLAN_LOG_ERROR("Freeing an skb (%p) w/ no frmmeta.\n", skb);
678         }
679
680         dev_kfree_skb(skb);
681         DBFEXIT;
682         return;
683 }