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