blob: 0a8f3960d4653ac0056812cb368a260d9b9634d7 [file] [log] [blame]
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -07001/* 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* --------------------------------------------------------------------
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +010051*
Pranjal Bhor4a552182016-01-19 01:03:49 +053052*================================================================
53*/
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070054
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>
Moritz Muehlenhoffae262302009-01-21 22:00:45 +010065#include <linux/byteorder/generic.h>
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070066
67#include <asm/byteorder.h>
68
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070069#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
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +010078static u8 oui_rfc1042[] = { 0x00, 0x00, 0x00 };
79static u8 oui_8021h[] = { 0x00, 0x00, 0xf8 };
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -070080
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
Pranjal Bhor4a552182016-01-19 01:03:49 +0530105*----------------------------------------------------------------
106*/
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530107int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv,
Edgardo Hames93df38e2010-07-30 22:51:55 -0300108 struct sk_buff *skb, union p80211_hdr *p80211_hdr,
Edgardo Hames51e48962010-07-31 13:06:52 -0300109 struct p80211_metawep *p80211_wep)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700110{
Ebru Akagunduzf474f5e2014-10-25 13:16:42 +0300111 __le16 fc;
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100112 u16 proto;
Edgardo Hames51e48962010-07-31 13:06:52 -0300113 struct wlan_ethhdr e_hdr;
114 struct wlan_llc *e_llc;
115 struct wlan_snap *e_snap;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700116 int foo;
117
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700118 memcpy(&e_hdr, skb->data, sizeof(e_hdr));
119
120 if (skb->len <= 0) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100121 pr_debug("zero-length skb!\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700122 return 1;
123 }
124
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100125 if (ethconv == WLAN_ETHCONV_ENCAP) { /* simplest case */
126 pr_debug("ENCAP len: %d\n", skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700127 /* 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);
Hari Prasath Gujulan Elango4c6b0ec2015-06-15 11:48:53 +0000133 if (proto <= ETH_DATA_LEN) {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100134 pr_debug("802.3 len: %d\n", skb->len);
135 /* codes <= 1500 reserved for 802.3 lengths */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700136 /* it's 802.3, pass ether payload unchanged, */
137
138 /* trim off ethernet header */
Anish Bhatt242850f2015-09-04 14:00:30 -0700139 skb_pull(skb, ETH_HLEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700140
141 /* leave off any PAD octets. */
142 skb_trim(skb, proto);
143 } else {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100144 pr_debug("DIXII len: %d\n", skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700145 /* it's DIXII, time for some conversion */
146
147 /* trim off ethernet header */
Anish Bhatt242850f2015-09-04 14:00:30 -0700148 skb_pull(skb, ETH_HLEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700149
150 /* tack on SNAP */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100151 e_snap =
Pranjal Bhor40defde2016-01-19 01:04:41 +0530152 (struct wlan_snap *)skb_push(skb,
Johan Meiring4eb28f72010-11-06 15:46:54 +0200153 sizeof(struct wlan_snap));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700154 e_snap->type = htons(proto);
Pranjal Bhor25845382016-01-19 01:04:09 +0530155 if (ethconv == WLAN_ETHCONV_8021h &&
156 p80211_stt_findproto(proto)) {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100157 memcpy(e_snap->oui, oui_8021h,
158 WLAN_IEEE_OUI_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700159 } else {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100160 memcpy(e_snap->oui, oui_rfc1042,
161 WLAN_IEEE_OUI_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700162 }
163
164 /* tack on llc */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100165 e_llc =
Pranjal Bhor40defde2016-01-19 01:04:41 +0530166 (struct wlan_llc *)skb_push(skb,
Johan Meiring4eb28f72010-11-06 15:46:54 +0200167 sizeof(struct wlan_llc));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700168 e_llc->dsap = 0xAA; /* SNAP, see IEEE 802 */
169 e_llc->ssap = 0xAA;
170 e_llc->ctl = 0x03;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700171 }
172 }
173
174 /* Set up the 802.11 header */
175 /* It's a data frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100176 fc = cpu_to_le16(WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
177 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700178
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100179 switch (wlandev->macmode) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700180 case WLAN_MACMODE_IBSS_STA:
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100181 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);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700184 break;
185 case WLAN_MACMODE_ESS_STA:
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100186 fc |= cpu_to_le16(WLAN_SET_FC_TODS(1));
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100187 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);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700190 break;
191 case WLAN_MACMODE_ESS_AP:
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100192 fc |= cpu_to_le16(WLAN_SET_FC_FROMDS(1));
Moritz Muehlenhoff28b17a42009-01-21 22:00:41 +0100193 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);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700196 break;
197 default:
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000198 netdev_err(wlandev->netdev,
199 "Error: Converting eth to wlan in unknown mode.\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700200 return 1;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700201 }
202
203 p80211_wep->data = NULL;
204
Pranjal Bhor25845382016-01-19 01:04:09 +0530205 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) &&
206 (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100207 /* XXXX need to pick keynum other than default? */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700208
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700209 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
Gujulan Elango, Hari Prasath (H.)4bff39d2015-05-13 14:35:25 +0000210 if (!p80211_wep->data)
211 return -ENOMEM;
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100212 foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
Greg Kroah-Hartmanb02957d2010-03-04 08:14:54 -0800213 skb->len,
Ruslan Pisarev5813b622010-03-15 21:27:42 +0200214 (wlandev->hostwep & HOSTWEP_DEFAULTKEY_MASK),
Andrew Elwell3f4b4e72010-02-18 23:56:13 +0100215 p80211_wep->iv, p80211_wep->icv);
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100216 if (foo) {
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000217 netdev_warn(wlandev->netdev,
Pranjal Bhor64ddba52016-01-19 01:04:56 +0530218 "Host en-WEP failed, dropping frame (%d).\n",
219 foo);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700220 return 2;
221 }
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100222 fc |= cpu_to_le16(WLAN_SET_FC_ISWEP(1));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700223 }
224
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100225 /* skb->nh.raw = skb->data; */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700226
227 p80211_hdr->a3.fc = fc;
228 p80211_hdr->a3.dur = 0;
229 p80211_hdr->a3.seq = 0;
230
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700231 return 0;
232}
233
234/* jkriegl: from orinoco, modified */
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530235static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
Edgardo Hames51e48962010-07-31 13:06:52 -0300236 struct p80211_rxmeta *rxmeta)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700237{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100238 int i;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700239
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100240 /* Gather wireless spy statistics: for each packet, compare the
Pranjal Bhor4a552182016-01-19 01:03:49 +0530241 * source address with out list, and if match, get the stats...
242 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700243
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100244 for (i = 0; i < wlandev->spy_number; i++) {
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100245 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700246 memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100247 wlandev->spy_stat[i].level = rxmeta->signal;
248 wlandev->spy_stat[i].noise = rxmeta->noise;
249 wlandev->spy_stat[i].qual =
250 (rxmeta->signal >
251 rxmeta->noise) ? (rxmeta->signal -
252 rxmeta->noise) : 0;
253 wlandev->spy_stat[i].updated = 0x7;
254 }
255 }
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700256}
257
258/*----------------------------------------------------------------
259* p80211pb_80211_to_ether
260*
261* Uses the contents of a received 802.11 frame and the etherconv
262* setting to build an ether frame.
263*
264* This function extracts the src and dest address from the 802.11
265* frame to use in the construction of the eth frame.
266*
267* Arguments:
268* ethconv Conversion type to perform
269* skb Packet buffer containing the 802.11 frame
270*
271* Returns:
272* 0 on success, non-zero otherwise
273*
274* Call context:
275* May be called in interrupt or non-interrupt context
Pranjal Bhor4a552182016-01-19 01:03:49 +0530276*----------------------------------------------------------------
277*/
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530278int skb_p80211_to_ether(wlandevice_t *wlandev, u32 ethconv,
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100279 struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700280{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100281 netdevice_t *netdev = wlandev->netdev;
282 u16 fc;
283 unsigned int payload_length;
284 unsigned int payload_offset;
Anish Bhatt242850f2015-09-04 14:00:30 -0700285 u8 daddr[ETH_ALEN];
286 u8 saddr[ETH_ALEN];
Edgardo Hames93df38e2010-07-30 22:51:55 -0300287 union p80211_hdr *w_hdr;
Edgardo Hames51e48962010-07-31 13:06:52 -0300288 struct wlan_ethhdr *e_hdr;
289 struct wlan_llc *e_llc;
290 struct wlan_snap *e_snap;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700291
292 int foo;
293
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700294 payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
295 payload_offset = WLAN_HDR_A3_LEN;
296
Pranjal Bhor40defde2016-01-19 01:04:41 +0530297 w_hdr = (union p80211_hdr *)skb->data;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700298
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100299 /* setup some vars for convenience */
Moritz Muehlenhoffae262302009-01-21 22:00:45 +0100300 fc = le16_to_cpu(w_hdr->a3.fc);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100301 if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
Anish Bhatt242850f2015-09-04 14:00:30 -0700302 ether_addr_copy(daddr, w_hdr->a3.a1);
303 ether_addr_copy(saddr, w_hdr->a3.a2);
Pranjal Bhor25845382016-01-19 01:04:09 +0530304 } else if ((WLAN_GET_FC_TODS(fc) == 0) &&
305 (WLAN_GET_FC_FROMDS(fc) == 1)) {
Anish Bhatt242850f2015-09-04 14:00:30 -0700306 ether_addr_copy(daddr, w_hdr->a3.a1);
307 ether_addr_copy(saddr, w_hdr->a3.a3);
Pranjal Bhor25845382016-01-19 01:04:09 +0530308 } else if ((WLAN_GET_FC_TODS(fc) == 1) &&
309 (WLAN_GET_FC_FROMDS(fc) == 0)) {
Anish Bhatt242850f2015-09-04 14:00:30 -0700310 ether_addr_copy(daddr, w_hdr->a3.a3);
311 ether_addr_copy(saddr, w_hdr->a3.a2);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700312 } else {
313 payload_offset = WLAN_HDR_A4_LEN;
Roel Kluin1f9e9ce2008-12-03 00:06:39 +0100314 if (payload_length < WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN) {
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000315 netdev_err(netdev, "A4 frame too short!\n");
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700316 return 1;
317 }
Roel Kluin1f9e9ce2008-12-03 00:06:39 +0100318 payload_length -= (WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN);
Anish Bhatt242850f2015-09-04 14:00:30 -0700319 ether_addr_copy(daddr, w_hdr->a4.a3);
320 ether_addr_copy(saddr, w_hdr->a4.a4);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700321 }
322
323 /* perform de-wep if necessary.. */
Pranjal Bhor25845382016-01-19 01:04:09 +0530324 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) &&
325 WLAN_GET_FC_ISWEP(fc) &&
326 (wlandev->hostwep & HOSTWEP_DECRYPT)) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700327 if (payload_length <= 8) {
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000328 netdev_err(netdev,
329 "WEP frame too short (%u).\n", skb->len);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700330 return 1;
331 }
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100332 foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
Pranjal Bhor64ddba52016-01-19 01:04:56 +0530333 payload_length - 8, -1,
334 skb->data + payload_offset,
335 skb->data + payload_offset +
336 payload_length - 4);
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100337 if (foo) {
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700338 /* de-wep failed, drop skb. */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530339 pr_debug("Host de-WEP failed, dropping frame (%d).\n",
340 foo);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700341 wlandev->rx.decrypt_err++;
342 return 2;
343 }
344
345 /* subtract the IV+ICV length off the payload */
346 payload_length -= 8;
347 /* chop off the IV */
348 skb_pull(skb, 4);
349 /* chop off the ICV. */
350 skb_trim(skb, skb->len - 4);
351
352 wlandev->rx.decrypt++;
353 }
354
Pranjal Bhor40defde2016-01-19 01:04:41 +0530355 e_hdr = (struct wlan_ethhdr *)(skb->data + payload_offset);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700356
Pranjal Bhor40defde2016-01-19 01:04:41 +0530357 e_llc = (struct wlan_llc *)(skb->data + payload_offset);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100358 e_snap =
Pranjal Bhor40defde2016-01-19 01:04:41 +0530359 (struct wlan_snap *)(skb->data + payload_offset +
Johan Meiring4eb28f72010-11-06 15:46:54 +0200360 sizeof(struct wlan_llc));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700361
362 /* Test for the various encodings */
Edgardo Hames51e48962010-07-31 13:06:52 -0300363 if ((payload_length >= sizeof(struct wlan_ethhdr)) &&
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100364 (e_llc->dsap != 0xaa || e_llc->ssap != 0xaa) &&
Anish Bhatt242850f2015-09-04 14:00:30 -0700365 ((!ether_addr_equal_unaligned(daddr, e_hdr->daddr)) ||
366 (!ether_addr_equal_unaligned(saddr, e_hdr->saddr)))) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100367 pr_debug("802.3 ENCAP len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700368 /* 802.3 Encapsulated */
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000369 /* Test for an overlength frame */
Anish Bhatt242850f2015-09-04 14:00:30 -0700370 if (payload_length > (netdev->mtu + ETH_HLEN)) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000371 /* A bogus length ethfrm has been encap'd. */
372 /* Is someone trying an oflow attack? */
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000373 netdev_err(netdev, "ENCAP frame too large (%d > %d)\n",
Pranjal Bhor64ddba52016-01-19 01:04:56 +0530374 payload_length, netdev->mtu + ETH_HLEN);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000375 return 1;
376 }
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700377
378 /* Chop off the 802.11 header. it's already sane. */
379 skb_pull(skb, payload_offset);
380 /* chop off the 802.11 CRC */
381 skb_trim(skb, skb->len - WLAN_CRC_LEN);
382
Johan Meiring4eb28f72010-11-06 15:46:54 +0200383 } else if ((payload_length >= sizeof(struct wlan_llc) +
Pranjal Bhor25845382016-01-19 01:04:09 +0530384 sizeof(struct wlan_snap)) &&
385 (e_llc->dsap == 0xaa) &&
386 (e_llc->ssap == 0xaa) &&
387 (e_llc->ctl == 0x03) &&
388 (((memcmp(e_snap->oui, oui_rfc1042,
389 WLAN_IEEE_OUI_LEN) == 0) &&
390 (ethconv == WLAN_ETHCONV_8021h) &&
391 (p80211_stt_findproto(le16_to_cpu(e_snap->type)))) ||
392 (memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) !=
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100393 0))) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100394 pr_debug("SNAP+RFC1042 len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700395 /* it's a SNAP + RFC1042 frame && protocol is in STT */
396 /* build 802.3 + RFC1042 */
397
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000398 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100399 if (payload_length > netdev->mtu) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000400 /* A bogus length ethfrm has been sent. */
401 /* Is someone trying an oflow attack? */
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000402 netdev_err(netdev, "SNAP frame too large (%d > %d)\n",
Pranjal Bhor64ddba52016-01-19 01:04:56 +0530403 payload_length, netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000404 return 1;
405 }
406
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700407 /* chop 802.11 header from skb. */
408 skb_pull(skb, payload_offset);
409
410 /* create 802.3 header at beginning of skb. */
Anish Bhatt242850f2015-09-04 14:00:30 -0700411 e_hdr = (struct wlan_ethhdr *)skb_push(skb, ETH_HLEN);
412 ether_addr_copy(e_hdr->daddr, daddr);
413 ether_addr_copy(e_hdr->saddr, saddr);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700414 e_hdr->type = htons(payload_length);
415
416 /* chop off the 802.11 CRC */
417 skb_trim(skb, skb->len - WLAN_CRC_LEN);
418
Johan Meiring4eb28f72010-11-06 15:46:54 +0200419 } else if ((payload_length >= sizeof(struct wlan_llc) +
Pranjal Bhor25845382016-01-19 01:04:09 +0530420 sizeof(struct wlan_snap)) &&
421 (e_llc->dsap == 0xaa) &&
422 (e_llc->ssap == 0xaa) &&
423 (e_llc->ctl == 0x03)) {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100424 pr_debug("802.1h/RFC1042 len: %d\n", payload_length);
Johan Meiring4eb28f72010-11-06 15:46:54 +0200425 /* it's an 802.1h frame || (an RFC1042 && protocol not in STT)
Pranjal Bhor4a552182016-01-19 01:03:49 +0530426 * build a DIXII + RFC894
427 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700428
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000429 /* Test for an overlength frame */
Johan Meiring4eb28f72010-11-06 15:46:54 +0200430 if ((payload_length - sizeof(struct wlan_llc) -
431 sizeof(struct wlan_snap))
432 > netdev->mtu) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000433 /* A bogus length ethfrm has been sent. */
434 /* Is someone trying an oflow attack? */
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000435 netdev_err(netdev, "DIXII frame too large (%ld > %d)\n",
Pranjal Bhor64ddba52016-01-19 01:04:56 +0530436 (long int)(payload_length -
437 sizeof(struct wlan_llc) -
438 sizeof(struct wlan_snap)), netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000439 return 1;
440 }
441
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700442 /* chop 802.11 header from skb. */
443 skb_pull(skb, payload_offset);
444
445 /* chop llc header from skb. */
Edgardo Hames51e48962010-07-31 13:06:52 -0300446 skb_pull(skb, sizeof(struct wlan_llc));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700447
448 /* chop snap header from skb. */
Edgardo Hames51e48962010-07-31 13:06:52 -0300449 skb_pull(skb, sizeof(struct wlan_snap));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700450
451 /* create 802.3 header at beginning of skb. */
Anish Bhatt242850f2015-09-04 14:00:30 -0700452 e_hdr = (struct wlan_ethhdr *)skb_push(skb, ETH_HLEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700453 e_hdr->type = e_snap->type;
Anish Bhatt242850f2015-09-04 14:00:30 -0700454 ether_addr_copy(e_hdr->daddr, daddr);
455 ether_addr_copy(e_hdr->saddr, saddr);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700456
457 /* chop off the 802.11 CRC */
458 skb_trim(skb, skb->len - WLAN_CRC_LEN);
459 } else {
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100460 pr_debug("NON-ENCAP len: %d\n", payload_length);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700461 /* any NON-ENCAP */
462 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
463 /* build an 802.3 frame */
464 /* allocate space and setup hostbuf */
465
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000466 /* Test for an overlength frame */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100467 if (payload_length > netdev->mtu) {
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000468 /* A bogus length ethfrm has been sent. */
469 /* Is someone trying an oflow attack? */
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000470 netdev_err(netdev, "OTHER frame too large (%d > %d)\n",
Pranjal Bhor64ddba52016-01-19 01:04:56 +0530471 payload_length, netdev->mtu);
Richard Kennedy33ce0ca2008-11-03 11:24:54 +0000472 return 1;
473 }
474
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700475 /* Chop off the 802.11 header. */
476 skb_pull(skb, payload_offset);
477
478 /* create 802.3 header at beginning of skb. */
Anish Bhatt242850f2015-09-04 14:00:30 -0700479 e_hdr = (struct wlan_ethhdr *)skb_push(skb, ETH_HLEN);
480 ether_addr_copy(e_hdr->daddr, daddr);
481 ether_addr_copy(e_hdr->saddr, saddr);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700482 e_hdr->type = htons(payload_length);
483
484 /* chop off the 802.11 CRC */
485 skb_trim(skb, skb->len - WLAN_CRC_LEN);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700486 }
487
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100488 /*
489 * Note that eth_type_trans() expects an skb w/ skb->data pointing
490 * at the MAC header, it then sets the following skb members:
491 * skb->mac_header,
492 * skb->data, and
493 * skb->pkt_type.
494 * It then _returns_ the value that _we're_ supposed to stuff in
495 * skb->protocol. This is nuts.
496 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700497 skb->protocol = eth_type_trans(skb, netdev);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700498
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100499 /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700500 /* jkriegl: only process signal/noise if requested by iwspy */
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100501 if (wlandev->spy_number)
502 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source,
503 P80211SKB_RXMETA(skb));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700504
505 /* Free the metadata */
506 p80211skb_rxmeta_detach(skb);
507
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700508 return 0;
509}
510
511/*----------------------------------------------------------------
512* p80211_stt_findproto
513*
514* Searches the 802.1h Selective Translation Table for a given
515* protocol.
516*
517* Arguments:
Masanari Iida1a6dfce2014-12-01 00:29:00 +0900518* proto protocol number (in host order) to search for.
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700519*
520* Returns:
521* 1 - if the table is empty or a match is found.
522* 0 - if the table is non-empty and a match is not found.
523*
524* Call context:
525* May be called in interrupt or non-interrupt context
Pranjal Bhor4a552182016-01-19 01:03:49 +0530526*----------------------------------------------------------------
527*/
Solomon Peachyaaad4302008-10-29 10:42:53 -0400528int p80211_stt_findproto(u16 proto)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700529{
530 /* Always return found for now. This is the behavior used by the */
Pranjal Bhor4a552182016-01-19 01:03:49 +0530531 /* Zoom Win95 driver when 802.1h mode is selected */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700532 /* TODO: If necessary, add an actual search we'll probably
Pranjal Bhor4a552182016-01-19 01:03:49 +0530533 * need this to match the CMAC's way of doing things.
534 * Need to do some testing to confirm.
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100535 */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700536
Hari Prasath Gujulan Elango4c6b0ec2015-06-15 11:48:53 +0000537 if (proto == ETH_P_AARP) /* APPLETALK */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700538 return 1;
539
540 return 0;
541}
542
543/*----------------------------------------------------------------
544* p80211skb_rxmeta_detach
545*
546* Disconnects the frmmeta and rxmeta from an skb.
547*
548* Arguments:
549* wlandev The wlandev this skb belongs to.
550* skb The skb we're attaching to.
551*
552* Returns:
553* 0 on success, non-zero otherwise
554*
555* Call context:
556* May be called in interrupt or non-interrupt context
Pranjal Bhor4a552182016-01-19 01:03:49 +0530557*----------------------------------------------------------------
558*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100559void p80211skb_rxmeta_detach(struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700560{
Edgardo Hames51e48962010-07-31 13:06:52 -0300561 struct p80211_rxmeta *rxmeta;
562 struct p80211_frmmeta *frmmeta;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700563
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700564 /* Sanity checks */
Pranjal Bhord2305432016-01-19 01:05:13 +0530565 if (!skb) { /* bad skb */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100566 pr_debug("Called w/ null skb.\n");
Devendra Naga311e24f2012-09-09 18:40:59 +0530567 return;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700568 }
569 frmmeta = P80211SKB_FRMMETA(skb);
Pranjal Bhord2305432016-01-19 01:05:13 +0530570 if (!frmmeta) { /* no magic */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100571 pr_debug("Called w/ bad frmmeta magic.\n");
Devendra Naga311e24f2012-09-09 18:40:59 +0530572 return;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700573 }
574 rxmeta = frmmeta->rx;
Pranjal Bhord2305432016-01-19 01:05:13 +0530575 if (!rxmeta) { /* bad meta ptr */
Moritz Muehlenhoffa7cf7ba2009-02-08 02:01:00 +0100576 pr_debug("Called w/ bad rxmeta ptr.\n");
Devendra Naga311e24f2012-09-09 18:40:59 +0530577 return;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700578 }
579
580 /* Free rxmeta */
581 kfree(rxmeta);
582
583 /* Clear skb->cb */
584 memset(skb->cb, 0, sizeof(skb->cb));
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700585}
586
587/*----------------------------------------------------------------
588* p80211skb_rxmeta_attach
589*
590* Allocates a p80211rxmeta structure, initializes it, and attaches
591* it to an skb.
592*
593* Arguments:
594* wlandev The wlandev this skb belongs to.
595* skb The skb we're attaching to.
596*
597* Returns:
598* 0 on success, non-zero otherwise
599*
600* Call context:
601* May be called in interrupt or non-interrupt context
Pranjal Bhor4a552182016-01-19 01:03:49 +0530602*----------------------------------------------------------------
603*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100604int p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700605{
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100606 int result = 0;
Edgardo Hames51e48962010-07-31 13:06:52 -0300607 struct p80211_rxmeta *rxmeta;
608 struct p80211_frmmeta *frmmeta;
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700609
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700610 /* If these already have metadata, we error out! */
Pranjal Bhord2305432016-01-19 01:05:13 +0530611 if (P80211SKB_RXMETA(skb)) {
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000612 netdev_err(wlandev->netdev,
613 "%s: RXmeta already attached!\n", wlandev->name);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700614 result = 0;
615 goto exit;
616 }
617
618 /* Allocate the rxmeta */
Edgardo Hames51e48962010-07-31 13:06:52 -0300619 rxmeta = kzalloc(sizeof(struct p80211_rxmeta), GFP_ATOMIC);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700620
Pranjal Bhord2305432016-01-19 01:05:13 +0530621 if (!rxmeta) {
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000622 netdev_err(wlandev->netdev,
623 "%s: Failed to allocate rxmeta.\n", wlandev->name);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700624 result = 1;
625 goto exit;
626 }
627
628 /* Initialize the rxmeta */
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700629 rxmeta->wlandev = wlandev;
630 rxmeta->hosttime = jiffies;
631
632 /* Overlay a frmmeta_t onto skb->cb */
Edgardo Hames51e48962010-07-31 13:06:52 -0300633 memset(skb->cb, 0, sizeof(struct p80211_frmmeta));
Pranjal Bhor40defde2016-01-19 01:04:41 +0530634 frmmeta = (struct p80211_frmmeta *)(skb->cb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700635 frmmeta->magic = P80211_FRMMETA_MAGIC;
636 frmmeta->rx = rxmeta;
637exit:
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700638 return result;
639}
640
641/*----------------------------------------------------------------
642* p80211skb_free
643*
644* Frees an entire p80211skb by checking and freeing the meta struct
645* and then freeing the skb.
646*
647* Arguments:
648* wlandev The wlandev this skb belongs to.
649* skb The skb we're attaching to.
650*
651* Returns:
652* 0 on success, non-zero otherwise
653*
654* Call context:
655* May be called in interrupt or non-interrupt context
Pranjal Bhor4a552182016-01-19 01:03:49 +0530656*----------------------------------------------------------------
657*/
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100658void p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700659{
Edgardo Hames51e48962010-07-31 13:06:52 -0300660 struct p80211_frmmeta *meta;
Moritz Muehlenhoff8a251b52009-01-21 22:00:44 +0100661
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700662 meta = P80211SKB_FRMMETA(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100663 if (meta && meta->rx)
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700664 p80211skb_rxmeta_detach(skb);
Moritz Muehlenhoff82eaca72009-02-08 02:20:56 +0100665 else
Vitaly Osipov02d9b1e2014-05-18 16:59:36 +1000666 netdev_err(wlandev->netdev,
667 "Freeing an skb (%p) w/ no frmmeta.\n", skb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700668 dev_kfree_skb(skb);
Greg Kroah-Hartman00b3ed12008-10-02 11:29:28 -0700669}