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