Staging: wlan-ng: multiple safe style cleanups
[pandora-kernel.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
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>
64 #include <linux/byteorder/generic.h>
65
66 #include <asm/byteorder.h>
67
68 #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
77 static u8 oui_rfc1042[] = { 0x00, 0x00, 0x00 };
78 static u8 oui_8021h[] = { 0x00, 0x00, 0xf8 };
79
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 ----------------------------------------------------------------*/
105 int 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)
108 {
109
110         u16 fc;
111         u16 proto;
112         wlan_ethhdr_t e_hdr;
113         wlan_llc_t *e_llc;
114         wlan_snap_t *e_snap;
115         int foo;
116
117         memcpy(&e_hdr, skb->data, sizeof(e_hdr));
118
119         if (skb->len <= 0) {
120                 pr_debug("zero-length skb!\n");
121                 return 1;
122         }
123
124         if (ethconv == WLAN_ETHCONV_ENCAP) {    /* simplest case */
125                 pr_debug("ENCAP len: %d\n", skb->len);
126                 /* 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);
132                 if (proto <= 1500) {
133                         pr_debug("802.3 len: %d\n", skb->len);
134                         /* codes <= 1500 reserved for 802.3 lengths */
135                         /* 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 {
143                         pr_debug("DIXII len: %d\n", skb->len);
144                         /* 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 */
150                         e_snap =
151                             (wlan_snap_t *) skb_push(skb, sizeof(wlan_snap_t));
152                         e_snap->type = htons(proto);
153                         if (ethconv == WLAN_ETHCONV_8021h
154                             && p80211_stt_findproto(proto)) {
155                                 memcpy(e_snap->oui, oui_8021h,
156                                        WLAN_IEEE_OUI_LEN);
157                         } else {
158                                 memcpy(e_snap->oui, oui_rfc1042,
159                                        WLAN_IEEE_OUI_LEN);
160                         }
161
162                         /* tack on llc */
163                         e_llc =
164                             (wlan_llc_t *) skb_push(skb, sizeof(wlan_llc_t));
165                         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 */
174         fc = cpu_to_le16(WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
175                          WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
176
177         switch (wlandev->macmode) {
178         case WLAN_MACMODE_IBSS_STA:
179                 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);
182                 break;
183         case WLAN_MACMODE_ESS_STA:
184                 fc |= cpu_to_le16(WLAN_SET_FC_TODS(1));
185                 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);
188                 break;
189         case WLAN_MACMODE_ESS_AP:
190                 fc |= cpu_to_le16(WLAN_SET_FC_FROMDS(1));
191                 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);
194                 break;
195         default:
196                 printk(KERN_ERR
197                        "Error: Converting eth to wlan in unknown mode.\n");
198                 return 1;
199                 break;
200         }
201
202         p80211_wep->data = NULL;
203
204         if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED)
205             && (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
206                 /* XXXX need to pick keynum other than default? */
207
208                 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
209                 foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
210                         skb->len,
211                         (wlandev->hostwep &
212                         HOSTWEP_DEFAULTKEY_MASK),
213                         p80211_wep->iv, p80211_wep->icv);
214                 if (foo) {
215                         printk(KERN_WARNING
216                                "Host en-WEP failed, dropping frame (%d).\n",
217                                foo);
218                         return 2;
219                 }
220                 fc |= cpu_to_le16(WLAN_SET_FC_ISWEP(1));
221         }
222
223         /*      skb->nh.raw = skb->data; */
224
225         p80211_hdr->a3.fc = fc;
226         p80211_hdr->a3.dur = 0;
227         p80211_hdr->a3.seq = 0;
228
229         return 0;
230 }
231
232 /* jkriegl: from orinoco, modified */
233 static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
234                                p80211_rxmeta_t *rxmeta)
235 {
236         int i;
237
238         /* Gather wireless spy statistics: for each packet, compare the
239          * source address with out list, and if match, get the stats... */
240
241         for (i = 0; i < wlandev->spy_number; i++) {
242
243                 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
244                         memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
245                         wlandev->spy_stat[i].level = rxmeta->signal;
246                         wlandev->spy_stat[i].noise = rxmeta->noise;
247                         wlandev->spy_stat[i].qual =
248                             (rxmeta->signal >
249                              rxmeta->noise) ? (rxmeta->signal -
250                                                rxmeta->noise) : 0;
251                         wlandev->spy_stat[i].updated = 0x7;
252                 }
253         }
254 }
255
256 /*----------------------------------------------------------------
257 * p80211pb_80211_to_ether
258 *
259 * Uses the contents of a received 802.11 frame and the etherconv
260 * setting to build an ether frame.
261 *
262 * This function extracts the src and dest address from the 802.11
263 * frame to use in the construction of the eth frame.
264 *
265 * Arguments:
266 *       ethconv         Conversion type to perform
267 *       skb             Packet buffer containing the 802.11 frame
268 *
269 * Returns:
270 *       0 on success, non-zero otherwise
271 *
272 * Call context:
273 *       May be called in interrupt or non-interrupt context
274 ----------------------------------------------------------------*/
275 int skb_p80211_to_ether(wlandevice_t *wlandev, u32 ethconv,
276                         struct sk_buff *skb)
277 {
278         netdevice_t *netdev = wlandev->netdev;
279         u16 fc;
280         unsigned int payload_length;
281         unsigned int payload_offset;
282         u8 daddr[WLAN_ETHADDR_LEN];
283         u8 saddr[WLAN_ETHADDR_LEN];
284         p80211_hdr_t *w_hdr;
285         wlan_ethhdr_t *e_hdr;
286         wlan_llc_t *e_llc;
287         wlan_snap_t *e_snap;
288
289         int foo;
290
291         payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
292         payload_offset = WLAN_HDR_A3_LEN;
293
294         w_hdr = (p80211_hdr_t *) skb->data;
295
296         /* setup some vars for convenience */
297         fc = le16_to_cpu(w_hdr->a3.fc);
298         if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
299                 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
300                 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
301         } else if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 1)) {
302                 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
303                 memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
304         } else if ((WLAN_GET_FC_TODS(fc) == 1) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
305                 memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
306                 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
307         } else {
308                 payload_offset = WLAN_HDR_A4_LEN;
309                 if (payload_length < WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN) {
310                         printk(KERN_ERR "A4 frame too short!\n");
311                         return 1;
312                 }
313                 payload_length -= (WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN);
314                 memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
315                 memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
316         }
317
318         /* perform de-wep if necessary.. */
319         if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && WLAN_GET_FC_ISWEP(fc)
320             && (wlandev->hostwep & HOSTWEP_DECRYPT)) {
321                 if (payload_length <= 8) {
322                         printk(KERN_ERR "WEP frame too short (%u).\n",
323                                skb->len);
324                         return 1;
325                 }
326                 foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
327                         payload_length - 8, -1,
328                         skb->data + payload_offset,
329                         skb->data + payload_offset +
330                         payload_length - 4);
331                 if (foo) {
332                         /* de-wep failed, drop skb. */
333                         pr_debug("Host de-WEP failed, dropping frame (%d).\n",
334                                  foo);
335                         wlandev->rx.decrypt_err++;
336                         return 2;
337                 }
338
339                 /* subtract the IV+ICV length off the payload */
340                 payload_length -= 8;
341                 /* chop off the IV */
342                 skb_pull(skb, 4);
343                 /* chop off the ICV. */
344                 skb_trim(skb, skb->len - 4);
345
346                 wlandev->rx.decrypt++;
347         }
348
349         e_hdr = (wlan_ethhdr_t *) (skb->data + payload_offset);
350
351         e_llc = (wlan_llc_t *) (skb->data + payload_offset);
352         e_snap =
353             (wlan_snap_t *) (skb->data + payload_offset + sizeof(wlan_llc_t));
354
355         /* Test for the various encodings */
356         if ((payload_length >= sizeof(wlan_ethhdr_t)) &&
357             (e_llc->dsap != 0xaa || e_llc->ssap != 0xaa) &&
358             ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
359              (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
360                 pr_debug("802.3 ENCAP len: %d\n", payload_length);
361                 /* 802.3 Encapsulated */
362                 /* Test for an overlength frame */
363                 if (payload_length > (netdev->mtu + WLAN_ETHHDR_LEN)) {
364                         /* A bogus length ethfrm has been encap'd. */
365                         /* Is someone trying an oflow attack? */
366                         printk(KERN_ERR "ENCAP frame too large (%d > %d)\n",
367                                payload_length, netdev->mtu + WLAN_ETHHDR_LEN);
368                         return 1;
369                 }
370
371                 /* Chop off the 802.11 header.  it's already sane. */
372                 skb_pull(skb, payload_offset);
373                 /* chop off the 802.11 CRC */
374                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
375
376         } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t))
377                    && (e_llc->dsap == 0xaa) && (e_llc->ssap == 0xaa)
378                    && (e_llc->ctl == 0x03)
379                    &&
380                    (((memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) == 0)
381                      && (ethconv == WLAN_ETHCONV_8021h)
382                      && (p80211_stt_findproto(le16_to_cpu(e_snap->type))))
383                     || (memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) !=
384                         0))) {
385                 pr_debug("SNAP+RFC1042 len: %d\n", payload_length);
386                 /* it's a SNAP + RFC1042 frame && protocol is in STT */
387                 /* build 802.3 + RFC1042 */
388
389                 /* Test for an overlength frame */
390                 if (payload_length > netdev->mtu) {
391                         /* A bogus length ethfrm has been sent. */
392                         /* Is someone trying an oflow attack? */
393                         printk(KERN_ERR "SNAP frame too large (%d > %d)\n",
394                                payload_length, netdev->mtu);
395                         return 1;
396                 }
397
398                 /* chop 802.11 header from skb. */
399                 skb_pull(skb, payload_offset);
400
401                 /* create 802.3 header at beginning of skb. */
402                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
403                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
404                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
405                 e_hdr->type = htons(payload_length);
406
407                 /* chop off the 802.11 CRC */
408                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
409
410         } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t))
411                    && (e_llc->dsap == 0xaa) && (e_llc->ssap == 0xaa)
412                    && (e_llc->ctl == 0x03)) {
413                 pr_debug("802.1h/RFC1042 len: %d\n", payload_length);
414                 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
415                 /* build a DIXII + RFC894 */
416
417                 /* Test for an overlength frame */
418                 if ((payload_length - sizeof(wlan_llc_t) - sizeof(wlan_snap_t))
419                     > netdev->mtu) {
420                         /* A bogus length ethfrm has been sent. */
421                         /* Is someone trying an oflow attack? */
422                         printk(KERN_ERR "DIXII frame too large (%ld > %d)\n",
423                                (long int)(payload_length - sizeof(wlan_llc_t) -
424                                           sizeof(wlan_snap_t)), netdev->mtu);
425                         return 1;
426                 }
427
428                 /* chop 802.11 header from skb. */
429                 skb_pull(skb, payload_offset);
430
431                 /* chop llc header from skb. */
432                 skb_pull(skb, sizeof(wlan_llc_t));
433
434                 /* chop snap header from skb. */
435                 skb_pull(skb, sizeof(wlan_snap_t));
436
437                 /* create 802.3 header at beginning of skb. */
438                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
439                 e_hdr->type = e_snap->type;
440                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
441                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
442
443                 /* chop off the 802.11 CRC */
444                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
445         } else {
446                 pr_debug("NON-ENCAP len: %d\n", payload_length);
447                 /* any NON-ENCAP */
448                 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
449                 /*  build an 802.3 frame */
450                 /* allocate space and setup hostbuf */
451
452                 /* Test for an overlength frame */
453                 if (payload_length > netdev->mtu) {
454                         /* A bogus length ethfrm has been sent. */
455                         /* Is someone trying an oflow attack? */
456                         printk(KERN_ERR "OTHER frame too large (%d > %d)\n",
457                                payload_length, netdev->mtu);
458                         return 1;
459                 }
460
461                 /* Chop off the 802.11 header. */
462                 skb_pull(skb, payload_offset);
463
464                 /* create 802.3 header at beginning of skb. */
465                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
466                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
467                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
468                 e_hdr->type = htons(payload_length);
469
470                 /* chop off the 802.11 CRC */
471                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
472
473         }
474
475         /*
476          * Note that eth_type_trans() expects an skb w/ skb->data pointing
477          * at the MAC header, it then sets the following skb members:
478          * skb->mac_header,
479          * skb->data, and
480          * skb->pkt_type.
481          * It then _returns_ the value that _we're_ supposed to stuff in
482          * skb->protocol.  This is nuts.
483          */
484         skb->protocol = eth_type_trans(skb, netdev);
485
486         /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
487         /* jkriegl: only process signal/noise if requested by iwspy */
488         if (wlandev->spy_number)
489                 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source,
490                                    P80211SKB_RXMETA(skb));
491
492         /* Free the metadata */
493         p80211skb_rxmeta_detach(skb);
494
495         return 0;
496 }
497
498 /*----------------------------------------------------------------
499 * p80211_stt_findproto
500 *
501 * Searches the 802.1h Selective Translation Table for a given
502 * protocol.
503 *
504 * Arguments:
505 *       proto   protocl number (in host order) to search for.
506 *
507 * Returns:
508 *       1 - if the table is empty or a match is found.
509 *       0 - if the table is non-empty and a match is not found.
510 *
511 * Call context:
512 *       May be called in interrupt or non-interrupt context
513 ----------------------------------------------------------------*/
514 int p80211_stt_findproto(u16 proto)
515 {
516         /* Always return found for now.  This is the behavior used by the */
517         /*  Zoom Win95 driver when 802.1h mode is selected */
518         /* TODO: If necessary, add an actual search we'll probably
519            need this to match the CMAC's way of doing things.
520            Need to do some testing to confirm.
521          */
522
523         if (proto == 0x80f3)    /* APPLETALK */
524                 return 1;
525
526         return 0;
527 }
528
529 /*----------------------------------------------------------------
530 * p80211skb_rxmeta_detach
531 *
532 * Disconnects the frmmeta and rxmeta from an skb.
533 *
534 * Arguments:
535 *       wlandev         The wlandev this skb belongs to.
536 *       skb             The skb we're attaching to.
537 *
538 * Returns:
539 *       0 on success, non-zero otherwise
540 *
541 * Call context:
542 *       May be called in interrupt or non-interrupt context
543 ----------------------------------------------------------------*/
544 void p80211skb_rxmeta_detach(struct sk_buff *skb)
545 {
546         p80211_rxmeta_t *rxmeta;
547         p80211_frmmeta_t *frmmeta;
548
549         /* Sanity checks */
550         if (skb == NULL) {      /* bad skb */
551                 pr_debug("Called w/ null skb.\n");
552                 goto exit;
553         }
554         frmmeta = P80211SKB_FRMMETA(skb);
555         if (frmmeta == NULL) {  /* no magic */
556                 pr_debug("Called w/ bad frmmeta magic.\n");
557                 goto exit;
558         }
559         rxmeta = frmmeta->rx;
560         if (rxmeta == NULL) {   /* bad meta ptr */
561                 pr_debug("Called w/ bad rxmeta ptr.\n");
562                 goto exit;
563         }
564
565         /* Free rxmeta */
566         kfree(rxmeta);
567
568         /* Clear skb->cb */
569         memset(skb->cb, 0, sizeof(skb->cb));
570 exit:
571         return;
572 }
573
574 /*----------------------------------------------------------------
575 * p80211skb_rxmeta_attach
576 *
577 * Allocates a p80211rxmeta structure, initializes it, and attaches
578 * it to an skb.
579 *
580 * Arguments:
581 *       wlandev         The wlandev this skb belongs to.
582 *       skb             The skb we're attaching to.
583 *
584 * Returns:
585 *       0 on success, non-zero otherwise
586 *
587 * Call context:
588 *       May be called in interrupt or non-interrupt context
589 ----------------------------------------------------------------*/
590 int p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
591 {
592         int result = 0;
593         p80211_rxmeta_t *rxmeta;
594         p80211_frmmeta_t *frmmeta;
595
596         /* If these already have metadata, we error out! */
597         if (P80211SKB_RXMETA(skb) != NULL) {
598                 printk(KERN_ERR "%s: RXmeta already attached!\n",
599                        wlandev->name);
600                 result = 0;
601                 goto exit;
602         }
603
604         /* Allocate the rxmeta */
605         rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
606
607         if (rxmeta == NULL) {
608                 printk(KERN_ERR "%s: Failed to allocate rxmeta.\n",
609                        wlandev->name);
610                 result = 1;
611                 goto exit;
612         }
613
614         /* Initialize the rxmeta */
615         memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
616         rxmeta->wlandev = wlandev;
617         rxmeta->hosttime = jiffies;
618
619         /* Overlay a frmmeta_t onto skb->cb */
620         memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
621         frmmeta = (p80211_frmmeta_t *) (skb->cb);
622         frmmeta->magic = P80211_FRMMETA_MAGIC;
623         frmmeta->rx = rxmeta;
624 exit:
625         return result;
626 }
627
628 /*----------------------------------------------------------------
629 * p80211skb_free
630 *
631 * Frees an entire p80211skb by checking and freeing the meta struct
632 * and then freeing the skb.
633 *
634 * Arguments:
635 *       wlandev         The wlandev this skb belongs to.
636 *       skb             The skb we're attaching to.
637 *
638 * Returns:
639 *       0 on success, non-zero otherwise
640 *
641 * Call context:
642 *       May be called in interrupt or non-interrupt context
643 ----------------------------------------------------------------*/
644 void p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
645 {
646         p80211_frmmeta_t *meta;
647
648         meta = P80211SKB_FRMMETA(skb);
649         if (meta && meta->rx)
650                 p80211skb_rxmeta_detach(skb);
651         else
652                 printk(KERN_ERR "Freeing an skb (%p) w/ no frmmeta.\n", skb);
653         dev_kfree_skb(skb);
654         return;
655 }