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