1 /* src/p80211/p80211conv.c
2 *
3 * Ether/802.11 conversions and packet buffer routines
4 *
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
6 * --------------------------------------------------------------------
7 *
8 * linux-wlan
9 *
10 *   The contents of this file are subject to the Mozilla Public
11 *   License Version 1.1 (the "License"); you may not use this file
12 *   except in compliance with the License. You may obtain a copy of
13 *   the License at http://www.mozilla.org/MPL/
14 *
15 *   Software distributed under the License is distributed on an "AS
16 *   IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 *   implied. See the License for the specific language governing
18 *   rights and limitations under the License.
19 *
20 *   Alternatively, the contents of this file may be used under the
21 *   terms of the GNU Public License version 2 (the "GPL"), in which
22 *   case the provisions of the GPL are applicable instead of the
23 *   above.  If you wish to allow the use of your version of this file
24 *   only under the terms of the GPL and not to allow others to use
25 *   your version of this file under the MPL, indicate your decision
26 *   by deleting the provisions above and replace them with the notice
27 *   and other provisions required by the GPL.  If you do not delete
28 *   the provisions above, a recipient may use your version of this
29 *   file under either the MPL or the GPL.
30 *
31 * --------------------------------------------------------------------
32 *
33 * Inquiries regarding the linux-wlan Open Source project can be
34 * made directly to:
35 *
36 * AbsoluteValue Systems Inc.
37 * info@linux-wlan.com
38 * http://www.linux-wlan.com
39 *
40 * --------------------------------------------------------------------
41 *
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
44 *
45 * --------------------------------------------------------------------
46 *
47 * This file defines the functions that perform Ethernet to/from
48 * 802.11 frame conversions.
49 *
50 * --------------------------------------------------------------------
51 *
52 *================================================================ */
53 
54 #include <linux/module.h>
55 #include <linux/kernel.h>
56 #include <linux/sched.h>
57 #include <linux/types.h>
58 #include <linux/skbuff.h>
59 #include <linux/slab.h>
60 #include <linux/wireless.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/if_ether.h>
64 #include <linux/byteorder/generic.h>
65 
66 #include <asm/byteorder.h>
67 
68 #include "p80211types.h"
69 #include "p80211hdr.h"
70 #include "p80211conv.h"
71 #include "p80211mgmt.h"
72 #include "p80211msg.h"
73 #include "p80211netdev.h"
74 #include "p80211ioctl.h"
75 #include "p80211req.h"
76 
77 static u8 oui_rfc1042[] = { 0x00, 0x00, 0x00 };
78 static u8 oui_8021h[] = { 0x00, 0x00, 0xf8 };
79 
80 /*----------------------------------------------------------------
81 * p80211pb_ether_to_80211
82 *
83 * Uses the contents of the ether frame and the etherconv setting
84 * to build the elements of the 802.11 frame.
85 *
86 * We don't actually set
87 * up the frame header here.  That's the MAC's job.  We're only handling
88 * conversion of DIXII or 802.3+LLC frames to something that works
89 * with 802.11.
90 *
91 * Note -- 802.11 header is NOT part of the skb.  Likewise, the 802.11
92 *         FCS is also not present and will need to be added elsewhere.
93 *
94 * Arguments:
95 *	ethconv		Conversion type to perform
96 *	skb		skbuff containing the ether frame
97 *       p80211_hdr      802.11 header
98 *
99 * Returns:
100 *	0 on success, non-zero otherwise
101 *
102 * Call context:
103 *	May be called in interrupt or non-interrupt context
104 ----------------------------------------------------------------*/
skb_ether_to_p80211(wlandevice_t * wlandev,u32 ethconv,struct sk_buff * skb,union p80211_hdr * p80211_hdr,struct p80211_metawep * p80211_wep)105 int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv,
106 			struct sk_buff *skb, union p80211_hdr *p80211_hdr,
107 			struct p80211_metawep *p80211_wep)
108 {
109 
110 	__le16 fc;
111 	u16 proto;
112 	struct wlan_ethhdr e_hdr;
113 	struct wlan_llc *e_llc;
114 	struct wlan_snap *e_snap;
115 	int foo;
116 
117 	memcpy(&e_hdr, skb->data, sizeof(e_hdr));
118 
119 	if (skb->len <= 0) {
120 		pr_debug("zero-length skb!\n");
121 		return 1;
122 	}
123 
124 	if (ethconv == WLAN_ETHCONV_ENCAP) {	/* simplest case */
125 		pr_debug("ENCAP len: %d\n", skb->len);
126 		/* here, we don't care what kind of ether frm. Just stick it */
127 		/*  in the 80211 payload */
128 		/* which is to say, leave the skb alone. */
129 	} else {
130 		/* step 1: classify ether frame, DIX or 802.3? */
131 		proto = ntohs(e_hdr.type);
132 		if (proto <= 1500) {
133 			pr_debug("802.3 len: %d\n", skb->len);
134 			/* codes <= 1500 reserved for 802.3 lengths */
135 			/* it's 802.3, pass ether payload unchanged,  */
136 
137 			/* trim off ethernet header */
138 			skb_pull(skb, WLAN_ETHHDR_LEN);
139 
140 			/*   leave off any PAD octets.  */
141 			skb_trim(skb, proto);
142 		} else {
143 			pr_debug("DIXII len: %d\n", skb->len);
144 			/* it's DIXII, time for some conversion */
145 
146 			/* trim off ethernet header */
147 			skb_pull(skb, WLAN_ETHHDR_LEN);
148 
149 			/* tack on SNAP */
150 			e_snap =
151 			    (struct wlan_snap *) skb_push(skb,
152 				sizeof(struct wlan_snap));
153 			e_snap->type = htons(proto);
154 			if (ethconv == WLAN_ETHCONV_8021h
155 			    && p80211_stt_findproto(proto)) {
156 				memcpy(e_snap->oui, oui_8021h,
157 				       WLAN_IEEE_OUI_LEN);
158 			} else {
159 				memcpy(e_snap->oui, oui_rfc1042,
160 				       WLAN_IEEE_OUI_LEN);
161 			}
162 
163 			/* tack on llc */
164 			e_llc =
165 			    (struct wlan_llc *) skb_push(skb,
166 				sizeof(struct wlan_llc));
167 			e_llc->dsap = 0xAA;	/* SNAP, see IEEE 802 */
168 			e_llc->ssap = 0xAA;
169 			e_llc->ctl = 0x03;
170 
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 		foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
211 				  skb->len,
212 				  (wlandev->hostwep & HOSTWEP_DEFAULTKEY_MASK),
213 				  p80211_wep->iv, p80211_wep->icv);
214 		if (foo) {
215 			netdev_warn(wlandev->netdev,
216 			       "Host en-WEP failed, dropping frame (%d).\n",
217 			       foo);
218 			return 2;
219 		}
220 		fc |= cpu_to_le16(WLAN_SET_FC_ISWEP(1));
221 	}
222 
223 	/*      skb->nh.raw = skb->data; */
224 
225 	p80211_hdr->a3.fc = fc;
226 	p80211_hdr->a3.dur = 0;
227 	p80211_hdr->a3.seq = 0;
228 
229 	return 0;
230 }
231 
232 /* jkriegl: from orinoco, modified */
orinoco_spy_gather(wlandevice_t * wlandev,char * mac,struct p80211_rxmeta * rxmeta)233 static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
234 			       struct p80211_rxmeta *rxmeta)
235 {
236 	int i;
237 
238 	/* Gather wireless spy statistics: for each packet, compare the
239 	 * source address with out list, and if match, get the stats... */
240 
241 	for (i = 0; i < wlandev->spy_number; i++) {
242 
243 		if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
244 			memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
245 			wlandev->spy_stat[i].level = rxmeta->signal;
246 			wlandev->spy_stat[i].noise = rxmeta->noise;
247 			wlandev->spy_stat[i].qual =
248 			    (rxmeta->signal >
249 			     rxmeta->noise) ? (rxmeta->signal -
250 					       rxmeta->noise) : 0;
251 			wlandev->spy_stat[i].updated = 0x7;
252 		}
253 	}
254 }
255 
256 /*----------------------------------------------------------------
257 * p80211pb_80211_to_ether
258 *
259 * Uses the contents of a received 802.11 frame and the etherconv
260 * setting to build an ether frame.
261 *
262 * This function extracts the src and dest address from the 802.11
263 * frame to use in the construction of the eth frame.
264 *
265 * Arguments:
266 *	ethconv		Conversion type to perform
267 *	skb		Packet buffer containing the 802.11 frame
268 *
269 * Returns:
270 *	0 on success, non-zero otherwise
271 *
272 * Call context:
273 *	May be called in interrupt or non-interrupt context
274 ----------------------------------------------------------------*/
skb_p80211_to_ether(wlandevice_t * wlandev,u32 ethconv,struct sk_buff * skb)275 int skb_p80211_to_ether(wlandevice_t *wlandev, u32 ethconv,
276 			struct sk_buff *skb)
277 {
278 	netdevice_t *netdev = wlandev->netdev;
279 	u16 fc;
280 	unsigned int payload_length;
281 	unsigned int payload_offset;
282 	u8 daddr[WLAN_ETHADDR_LEN];
283 	u8 saddr[WLAN_ETHADDR_LEN];
284 	union p80211_hdr *w_hdr;
285 	struct wlan_ethhdr *e_hdr;
286 	struct wlan_llc *e_llc;
287 	struct wlan_snap *e_snap;
288 
289 	int foo;
290 
291 	payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
292 	payload_offset = WLAN_HDR_A3_LEN;
293 
294 	w_hdr = (union p80211_hdr *) skb->data;
295 
296 	/* setup some vars for convenience */
297 	fc = le16_to_cpu(w_hdr->a3.fc);
298 	if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
299 		memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
300 		memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
301 	} else if ((WLAN_GET_FC_TODS(fc) == 0)
302 			&& (WLAN_GET_FC_FROMDS(fc) == 1)) {
303 		memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
304 		memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
305 	} else if ((WLAN_GET_FC_TODS(fc) == 1)
306 			&& (WLAN_GET_FC_FROMDS(fc) == 0)) {
307 		memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
308 		memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
309 	} else {
310 		payload_offset = WLAN_HDR_A4_LEN;
311 		if (payload_length < WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN) {
312 			netdev_err(netdev, "A4 frame too short!\n");
313 			return 1;
314 		}
315 		payload_length -= (WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN);
316 		memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
317 		memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
318 	}
319 
320 	/* perform de-wep if necessary.. */
321 	if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && 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 	    ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
362 	     (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
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 + WLAN_ETHHDR_LEN)) {
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 + WLAN_ETHHDR_LEN);
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 		   &&
385 		   (((memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) == 0)
386 		     && (ethconv == WLAN_ETHCONV_8021h)
387 		     && (p80211_stt_findproto(le16_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 = (struct wlan_ethhdr *) 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(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 		/* 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 = (struct wlan_ethhdr *) skb_push(skb, WLAN_ETHHDR_LEN);
448 		e_hdr->type = e_snap->type;
449 		memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
450 		memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
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 = (struct wlan_ethhdr *) skb_push(skb, WLAN_ETHHDR_LEN);
475 		memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
476 		memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
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 	/*
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 ----------------------------------------------------------------*/
p80211_stt_findproto(u16 proto)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 == 0x80f3)	/* 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 ----------------------------------------------------------------*/
p80211skb_rxmeta_detach(struct sk_buff * skb)553 void p80211skb_rxmeta_detach(struct sk_buff *skb)
554 {
555 	struct p80211_rxmeta *rxmeta;
556 	struct p80211_frmmeta *frmmeta;
557 
558 	/* Sanity checks */
559 	if (skb == NULL) {	/* bad skb */
560 		pr_debug("Called w/ null skb.\n");
561 		return;
562 	}
563 	frmmeta = P80211SKB_FRMMETA(skb);
564 	if (frmmeta == NULL) {	/* no magic */
565 		pr_debug("Called w/ bad frmmeta magic.\n");
566 		return;
567 	}
568 	rxmeta = frmmeta->rx;
569 	if (rxmeta == NULL) {	/* bad meta ptr */
570 		pr_debug("Called w/ bad rxmeta ptr.\n");
571 		return;
572 	}
573 
574 	/* Free rxmeta */
575 	kfree(rxmeta);
576 
577 	/* Clear skb->cb */
578 	memset(skb->cb, 0, sizeof(skb->cb));
579 }
580 
581 /*----------------------------------------------------------------
582 * p80211skb_rxmeta_attach
583 *
584 * Allocates a p80211rxmeta structure, initializes it, and attaches
585 * it to an skb.
586 *
587 * Arguments:
588 *	wlandev		The wlandev this skb belongs to.
589 *	skb		The skb we're attaching to.
590 *
591 * Returns:
592 *	0 on success, non-zero otherwise
593 *
594 * Call context:
595 *	May be called in interrupt or non-interrupt context
596 ----------------------------------------------------------------*/
p80211skb_rxmeta_attach(struct wlandevice * wlandev,struct sk_buff * skb)597 int p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
598 {
599 	int result = 0;
600 	struct p80211_rxmeta *rxmeta;
601 	struct p80211_frmmeta *frmmeta;
602 
603 	/* If these already have metadata, we error out! */
604 	if (P80211SKB_RXMETA(skb) != NULL) {
605 		netdev_err(wlandev->netdev,
606 			   "%s: RXmeta already attached!\n", wlandev->name);
607 		result = 0;
608 		goto exit;
609 	}
610 
611 	/* Allocate the rxmeta */
612 	rxmeta = kzalloc(sizeof(struct p80211_rxmeta), GFP_ATOMIC);
613 
614 	if (rxmeta == NULL) {
615 		netdev_err(wlandev->netdev,
616 			   "%s: Failed to allocate rxmeta.\n", wlandev->name);
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 ----------------------------------------------------------------*/
p80211skb_free(struct wlandevice * wlandev,struct sk_buff * skb)650 void p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
651 {
652 	struct p80211_frmmeta *meta;
653 
654 	meta = P80211SKB_FRMMETA(skb);
655 	if (meta && meta->rx)
656 		p80211skb_rxmeta_detach(skb);
657 	else
658 		netdev_err(wlandev->netdev,
659 			   "Freeing an skb (%p) w/ no frmmeta.\n", skb);
660 	dev_kfree_skb(skb);
661 }
662