d58f10f11d00b93f648584ccdfe4e41da704c671
[firefly-linux-kernel-4.4.55.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
385                 /* Chop off the 802.11 header.  it's already sane. */
386                 skb_pull(skb, payload_offset);
387                 /* chop off the 802.11 CRC */
388                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
389
390         } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
391                    (e_llc->dsap == 0xaa) &&
392                    (e_llc->ssap == 0xaa) &&
393                    (e_llc->ctl == 0x03) &&
394                    (((memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)==0) &&
395                     (ethconv == WLAN_ETHCONV_8021h) &&
396                     (p80211_stt_findproto(ieee2host16(e_snap->type)))) ||
397                     (memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)!=0)))
398         {
399                 WLAN_LOG_DEBUG(3, "SNAP+RFC1042 len: %d\n", payload_length);
400                 /* it's a SNAP + RFC1042 frame && protocol is in STT */
401                 /* build 802.3 + RFC1042 */
402
403                 /* chop 802.11 header from skb. */
404                 skb_pull(skb, payload_offset);
405
406                 /* create 802.3 header at beginning of skb. */
407                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
408                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
409                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
410                 e_hdr->type = htons(payload_length);
411
412                 /* chop off the 802.11 CRC */
413                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
414
415         }  else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
416                     (e_llc->dsap == 0xaa) &&
417                     (e_llc->ssap == 0xaa) &&
418                     (e_llc->ctl == 0x03) ) {
419                 WLAN_LOG_DEBUG(3, "802.1h/RFC1042 len: %d\n", payload_length);
420                 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
421                 /* build a DIXII + RFC894 */
422
423                 /* chop 802.11 header from skb. */
424                 skb_pull(skb, payload_offset);
425
426                 /* chop llc header from skb. */
427                 skb_pull(skb, sizeof(wlan_llc_t));
428
429                 /* chop snap header from skb. */
430                 skb_pull(skb, sizeof(wlan_snap_t));
431
432                 /* create 802.3 header at beginning of skb. */
433                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
434                 e_hdr->type = e_snap->type;
435                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
436                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
437
438                 /* chop off the 802.11 CRC */
439                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
440         } else {
441                 WLAN_LOG_DEBUG(3, "NON-ENCAP len: %d\n", payload_length);
442                 /* any NON-ENCAP */
443                 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
444                 /*  build an 802.3 frame */
445                 /* allocate space and setup hostbuf */
446
447                 /* Test for an overlength frame */
448                 if ( payload_length > netdev->mtu ) {
449                         /* A bogus length ethfrm has been sent. */
450                         /* Is someone trying an oflow attack? */
451                         WLAN_LOG_ERROR("OTHER frame too large (%d > %d)\n",
452                                 payload_length,
453                                 netdev->mtu);
454                         return 1;
455                 }
456
457                 /* Chop off the 802.11 header. */
458                 skb_pull(skb, payload_offset);
459
460                 /* create 802.3 header at beginning of skb. */
461                 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
462                 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
463                 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
464                 e_hdr->type = htons(payload_length);
465
466                 /* chop off the 802.11 CRC */
467                 skb_trim(skb, skb->len - WLAN_CRC_LEN);
468
469         }
470
471         skb->protocol = eth_type_trans(skb, netdev);
472         skb_reset_mac_header(skb);
473
474         /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
475         /* jkriegl: only process signal/noise if requested by iwspy */
476         if (wlandev->spy_number)
477                 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source, P80211SKB_RXMETA(skb));
478
479         /* Free the metadata */
480         p80211skb_rxmeta_detach(skb);
481
482         DBFEXIT;
483         return 0;
484 }
485
486 /*----------------------------------------------------------------
487 * p80211_stt_findproto
488 *
489 * Searches the 802.1h Selective Translation Table for a given
490 * protocol.
491 *
492 * Arguments:
493 *       proto   protocl number (in host order) to search for.
494 *
495 * Returns:
496 *       1 - if the table is empty or a match is found.
497 *       0 - if the table is non-empty and a match is not found.
498 *
499 * Call context:
500 *       May be called in interrupt or non-interrupt context
501 ----------------------------------------------------------------*/
502 int p80211_stt_findproto(UINT16 proto)
503 {
504         /* Always return found for now.  This is the behavior used by the */
505         /*  Zoom Win95 driver when 802.1h mode is selected */
506         /* TODO: If necessary, add an actual search we'll probably
507                  need this to match the CMAC's way of doing things.
508                  Need to do some testing to confirm.
509         */
510
511         if (proto == 0x80f3)  /* APPLETALK */
512                 return 1;
513
514         return 0;
515 }
516
517 /*----------------------------------------------------------------
518 * p80211skb_rxmeta_detach
519 *
520 * Disconnects the frmmeta and rxmeta from an skb.
521 *
522 * Arguments:
523 *       wlandev         The wlandev this skb belongs to.
524 *       skb             The skb we're attaching to.
525 *
526 * Returns:
527 *       0 on success, non-zero otherwise
528 *
529 * Call context:
530 *       May be called in interrupt or non-interrupt context
531 ----------------------------------------------------------------*/
532 void
533 p80211skb_rxmeta_detach(struct sk_buff *skb)
534 {
535         p80211_rxmeta_t         *rxmeta;
536         p80211_frmmeta_t        *frmmeta;
537
538         DBFENTER;
539         /* Sanity checks */
540         if ( skb==NULL ) {                      /* bad skb */
541                 WLAN_LOG_DEBUG(1, "Called w/ null skb.\n");
542                 goto exit;
543         }
544         frmmeta = P80211SKB_FRMMETA(skb);
545         if ( frmmeta == NULL ) {                /* no magic */
546                 WLAN_LOG_DEBUG(1, "Called w/ bad frmmeta magic.\n");
547                 goto exit;
548         }
549         rxmeta = frmmeta->rx;
550         if ( rxmeta == NULL ) {                 /* bad meta ptr */
551                 WLAN_LOG_DEBUG(1, "Called w/ bad rxmeta ptr.\n");
552                 goto exit;
553         }
554
555         /* Free rxmeta */
556         kfree(rxmeta);
557
558         /* Clear skb->cb */
559         memset(skb->cb, 0, sizeof(skb->cb));
560 exit:
561         DBFEXIT;
562         return;
563 }
564
565 /*----------------------------------------------------------------
566 * p80211skb_rxmeta_attach
567 *
568 * Allocates a p80211rxmeta structure, initializes it, and attaches
569 * it to an skb.
570 *
571 * Arguments:
572 *       wlandev         The wlandev this skb belongs to.
573 *       skb             The skb we're attaching to.
574 *
575 * Returns:
576 *       0 on success, non-zero otherwise
577 *
578 * Call context:
579 *       May be called in interrupt or non-interrupt context
580 ----------------------------------------------------------------*/
581 int
582 p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
583 {
584         int                     result = 0;
585         p80211_rxmeta_t         *rxmeta;
586         p80211_frmmeta_t        *frmmeta;
587
588         DBFENTER;
589
590         /* If these already have metadata, we error out! */
591         if (P80211SKB_RXMETA(skb) != NULL) {
592                 WLAN_LOG_ERROR("%s: RXmeta already attached!\n",
593                                 wlandev->name);
594                 result = 0;
595                 goto exit;
596         }
597
598         /* Allocate the rxmeta */
599         rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
600
601         if ( rxmeta == NULL ) {
602                 WLAN_LOG_ERROR("%s: Failed to allocate rxmeta.\n",
603                                 wlandev->name);
604                 result = 1;
605                 goto exit;
606         }
607
608         /* Initialize the rxmeta */
609         memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
610         rxmeta->wlandev = wlandev;
611         rxmeta->hosttime = jiffies;
612
613         /* Overlay a frmmeta_t onto skb->cb */
614         memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
615         frmmeta = (p80211_frmmeta_t*)(skb->cb);
616         frmmeta->magic = P80211_FRMMETA_MAGIC;
617         frmmeta->rx = rxmeta;
618 exit:
619         DBFEXIT;
620         return result;
621 }
622
623 /*----------------------------------------------------------------
624 * p80211skb_free
625 *
626 * Frees an entire p80211skb by checking and freeing the meta struct
627 * and then freeing the skb.
628 *
629 * Arguments:
630 *       wlandev         The wlandev this skb belongs to.
631 *       skb             The skb we're attaching to.
632 *
633 * Returns:
634 *       0 on success, non-zero otherwise
635 *
636 * Call context:
637 *       May be called in interrupt or non-interrupt context
638 ----------------------------------------------------------------*/
639 void
640 p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
641 {
642         p80211_frmmeta_t        *meta;
643         DBFENTER;
644         meta = P80211SKB_FRMMETA(skb);
645         if ( meta && meta->rx) {
646                 p80211skb_rxmeta_detach(skb);
647         } else {
648                 WLAN_LOG_ERROR("Freeing an skb (%p) w/ no frmmeta.\n", skb);
649         }
650
651         dev_kfree_skb(skb);
652         DBFEXIT;
653         return;
654 }