blob: 37e9891605b43cba56d3afbb1d625d747d4e782b [file] [log] [blame]
Johannes Berg571ecf62007-07-27 15:43:22 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
S.Çağlar Onurab466232008-02-14 17:36:47 +020012#include <linux/jiffies.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020013#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040017#include <linux/rcupdate.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020018#include <net/mac80211.h>
19#include <net/ieee80211_radiotap.h>
20
21#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020022#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040023#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010024#include "mesh.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020025#include "wep.h"
26#include "wpa.h"
27#include "tkip.h"
28#include "wme.h"
29
Johannes Berga02ae752009-11-16 12:00:40 +010030static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
31 struct tid_ampdu_rx *tid_agg_rx,
32 u16 head_seq_num);
33
Johannes Bergb2e77712007-09-26 15:19:39 +020034/*
35 * monitor mode reception
36 *
37 * This function cleans up the SKB, i.e. it removes all the stuff
38 * only useful for monitoring.
39 */
40static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
Johannes Berg0869aea2009-10-28 10:03:35 +010041 struct sk_buff *skb)
Johannes Bergb2e77712007-09-26 15:19:39 +020042{
Johannes Bergb2e77712007-09-26 15:19:39 +020043 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
44 if (likely(skb->len > FCS_LEN))
45 skb_trim(skb, skb->len - FCS_LEN);
46 else {
47 /* driver bug */
48 WARN_ON(1);
49 dev_kfree_skb(skb);
50 skb = NULL;
51 }
52 }
53
54 return skb;
55}
56
Johannes Bergf1d58c22009-06-17 13:13:00 +020057static inline int should_drop_frame(struct sk_buff *skb,
Johannes Berg0869aea2009-10-28 10:03:35 +010058 int present_fcs_len)
Johannes Bergb2e77712007-09-26 15:19:39 +020059{
Johannes Bergf1d58c22009-06-17 13:13:00 +020060 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Harvey Harrison182503a2008-06-22 16:45:29 -070061 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergb2e77712007-09-26 15:19:39 +020062
63 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
64 return 1;
Johannes Berg0869aea2009-10-28 10:03:35 +010065 if (unlikely(skb->len < 16 + present_fcs_len))
Johannes Bergb2e77712007-09-26 15:19:39 +020066 return 1;
Harvey Harrison87228f52008-06-11 14:21:59 -070067 if (ieee80211_is_ctl(hdr->frame_control) &&
68 !ieee80211_is_pspoll(hdr->frame_control) &&
69 !ieee80211_is_back_req(hdr->frame_control))
Johannes Bergb2e77712007-09-26 15:19:39 +020070 return 1;
71 return 0;
72}
73
Bruno Randolf601ae7f2008-05-08 19:22:43 +020074static int
75ieee80211_rx_radiotap_len(struct ieee80211_local *local,
76 struct ieee80211_rx_status *status)
77{
78 int len;
79
80 /* always present fields */
81 len = sizeof(struct ieee80211_radiotap_header) + 9;
82
83 if (status->flag & RX_FLAG_TSFT)
84 len += 8;
Johannes Berg7fee5372009-01-30 11:13:06 +010085 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
Bruno Randolf601ae7f2008-05-08 19:22:43 +020086 len += 1;
87 if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
88 len += 1;
89
90 if (len & 1) /* padding for RX_FLAGS if necessary */
91 len++;
92
Bruno Randolf601ae7f2008-05-08 19:22:43 +020093 return len;
94}
95
Johannes Bergd1c3a372009-01-07 00:26:10 +010096/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +020097 * ieee80211_add_rx_radiotap_header - add radiotap header
98 *
99 * add a radiotap header containing all the fields which the hardware provided.
100 */
101static void
102ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
103 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200104 struct ieee80211_rate *rate,
105 int rtap_len)
106{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200107 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200108 struct ieee80211_radiotap_header *rthdr;
109 unsigned char *pos;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100110 u16 rx_flags = 0;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200111
112 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
113 memset(rthdr, 0, rtap_len);
114
115 /* radiotap header, set always present flags */
116 rthdr->it_present =
117 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200118 (1 << IEEE80211_RADIOTAP_CHANNEL) |
119 (1 << IEEE80211_RADIOTAP_ANTENNA) |
120 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
121 rthdr->it_len = cpu_to_le16(rtap_len);
122
123 pos = (unsigned char *)(rthdr+1);
124
125 /* the order of the following fields is important */
126
127 /* IEEE80211_RADIOTAP_TSFT */
128 if (status->flag & RX_FLAG_TSFT) {
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100129 put_unaligned_le64(status->mactime, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200130 rthdr->it_present |=
131 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
132 pos += 8;
133 }
134
135 /* IEEE80211_RADIOTAP_FLAGS */
136 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
137 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100138 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
139 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200140 if (status->flag & RX_FLAG_SHORTPRE)
141 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200142 pos++;
143
144 /* IEEE80211_RADIOTAP_RATE */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200145 if (status->flag & RX_FLAG_HT) {
146 /*
147 * TODO: add following information into radiotap header once
148 * suitable fields are defined for it:
149 * - MCS index (status->rate_idx)
150 * - HT40 (status->flag & RX_FLAG_40MHZ)
151 * - short-GI (status->flag & RX_FLAG_SHORT_GI)
152 */
153 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200154 } else {
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200155 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200156 *pos = rate->bitrate / 5;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200157 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200158 pos++;
159
160 /* IEEE80211_RADIOTAP_CHANNEL */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100161 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200162 pos += 2;
163 if (status->band == IEEE80211_BAND_5GHZ)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100164 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
165 pos);
Bruno Randolf9deb1ae2008-07-30 17:20:06 +0200166 else if (rate->flags & IEEE80211_RATE_ERP_G)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100167 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
168 pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200169 else
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100170 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
171 pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200172 pos += 2;
173
174 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
175 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
176 *pos = status->signal;
177 rthdr->it_present |=
178 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
179 pos++;
180 }
181
182 /* IEEE80211_RADIOTAP_DBM_ANTNOISE */
183 if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
184 *pos = status->noise;
185 rthdr->it_present |=
186 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTNOISE);
187 pos++;
188 }
189
190 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
191
192 /* IEEE80211_RADIOTAP_ANTENNA */
193 *pos = status->antenna;
194 pos++;
195
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200196 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
197
198 /* IEEE80211_RADIOTAP_RX_FLAGS */
199 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100200 if ((pos - (u8 *)rthdr) & 1)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200201 pos++;
Johannes Bergaae89832009-03-13 12:52:10 +0100202 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100203 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
204 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200205 pos += 2;
206}
207
Johannes Bergb2e77712007-09-26 15:19:39 +0200208/*
209 * This function copies a received frame to all monitor interfaces and
210 * returns a cleaned-up SKB that no longer includes the FCS nor the
211 * radiotap header the driver might have added.
212 */
213static struct sk_buff *
214ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100215 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200216{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200217 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200218 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2e77712007-09-26 15:19:39 +0200219 int needed_headroom = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200220 struct sk_buff *skb, *skb2;
221 struct net_device *prev_dev = NULL;
222 int present_fcs_len = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200223
224 /*
225 * First, we may need to make a copy of the skb because
226 * (1) we need to modify it for radiotap (if not present), and
227 * (2) the other RX handlers will modify the skb we got.
228 *
229 * We don't need to, of course, if we aren't going to return
230 * the SKB because it has a bad FCS/PLCP checksum.
231 */
Johannes Berg0869aea2009-10-28 10:03:35 +0100232
233 /* room for the radiotap header based on driver features */
234 needed_headroom = ieee80211_rx_radiotap_len(local, status);
Johannes Bergb2e77712007-09-26 15:19:39 +0200235
236 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
237 present_fcs_len = FCS_LEN;
238
239 if (!local->monitors) {
Johannes Berg0869aea2009-10-28 10:03:35 +0100240 if (should_drop_frame(origskb, present_fcs_len)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200241 dev_kfree_skb(origskb);
242 return NULL;
243 }
244
Johannes Berg0869aea2009-10-28 10:03:35 +0100245 return remove_monitor_info(local, origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200246 }
247
Johannes Berg0869aea2009-10-28 10:03:35 +0100248 if (should_drop_frame(origskb, present_fcs_len)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200249 /* only need to expand headroom if necessary */
250 skb = origskb;
251 origskb = NULL;
252
253 /*
254 * This shouldn't trigger often because most devices have an
255 * RX header they pull before we get here, and that should
256 * be big enough for our radiotap information. We should
257 * probably export the length to drivers so that we can have
258 * them allocate enough headroom to start with.
259 */
260 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100261 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200262 dev_kfree_skb(skb);
263 return NULL;
264 }
265 } else {
266 /*
267 * Need to make a copy and possibly remove radiotap header
268 * and FCS from the original.
269 */
270 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
271
Johannes Berg0869aea2009-10-28 10:03:35 +0100272 origskb = remove_monitor_info(local, origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200273
274 if (!skb)
275 return origskb;
276 }
277
Johannes Berg0869aea2009-10-28 10:03:35 +0100278 /* prepend radiotap information */
279 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
Johannes Bergb2e77712007-09-26 15:19:39 +0200280
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100281 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200282 skb->ip_summed = CHECKSUM_UNNECESSARY;
283 skb->pkt_type = PACKET_OTHERHOST;
284 skb->protocol = htons(ETH_P_802_2);
285
286 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
287 if (!netif_running(sdata->dev))
288 continue;
289
Johannes Berg05c914f2008-09-11 00:01:58 +0200290 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
Johannes Bergb2e77712007-09-26 15:19:39 +0200291 continue;
292
Michael Wu3d30d942008-01-31 19:48:27 +0100293 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
294 continue;
295
Johannes Bergb2e77712007-09-26 15:19:39 +0200296 if (prev_dev) {
297 skb2 = skb_clone(skb, GFP_ATOMIC);
298 if (skb2) {
299 skb2->dev = prev_dev;
300 netif_rx(skb2);
301 }
302 }
303
304 prev_dev = sdata->dev;
305 sdata->dev->stats.rx_packets++;
306 sdata->dev->stats.rx_bytes += skb->len;
307 }
308
309 if (prev_dev) {
310 skb->dev = prev_dev;
311 netif_rx(skb);
312 } else
313 dev_kfree_skb(skb);
314
315 return origskb;
316}
317
318
Johannes Berg5cf121c2008-02-25 16:27:43 +0100319static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200320{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700321 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200322 int tid;
323
324 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700325 if (ieee80211_is_data_qos(hdr->frame_control)) {
326 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200327 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700328 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
329 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100330 rx->flags |= IEEE80211_RX_AMSDU;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200331 else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100332 rx->flags &= ~IEEE80211_RX_AMSDU;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200333 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200334 /*
335 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
336 *
337 * Sequence numbers for management frames, QoS data
338 * frames with a broadcast/multicast address in the
339 * Address 1 field, and all non-QoS data frames sent
340 * by QoS STAs are assigned using an additional single
341 * modulo-4096 counter, [...]
342 *
343 * We also use that counter for non-QoS STAs.
344 */
345 tid = NUM_RX_DATA_QUEUES - 1;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200346 }
Johannes Berg52865df2007-07-27 15:43:22 +0200347
Johannes Berg5cf121c2008-02-25 16:27:43 +0100348 rx->queue = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200349 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
350 * For now, set skb->priority to 0 for other cases. */
351 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100352}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200353
Johannes Bergd1c3a372009-01-07 00:26:10 +0100354/**
355 * DOC: Packet alignment
356 *
357 * Drivers always need to pass packets that are aligned to two-byte boundaries
358 * to the stack.
359 *
360 * Additionally, should, if possible, align the payload data in a way that
361 * guarantees that the contained IP header is aligned to a four-byte
362 * boundary. In the case of regular frames, this simply means aligning the
363 * payload to a four-byte boundary (because either the IP header is directly
364 * contained, or IV/RFC1042 headers that have a length divisible by four are
365 * in front of it).
366 *
367 * With A-MSDU frames, however, the payload data address must yield two modulo
368 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
369 * push the IP header further back to a multiple of four again. Thankfully, the
370 * specs were sane enough this time around to require padding each A-MSDU
371 * subframe to a length that is a multiple of four.
372 *
373 * Padding like Atheros hardware adds which is inbetween the 802.11 header and
374 * the payload is not supported, the driver is required to move the 802.11
375 * header to be directly in front of the payload in that case.
376 */
377static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100378{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700379 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +0100380 int hdrlen;
381
Johannes Bergd1c3a372009-01-07 00:26:10 +0100382#ifndef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
383 return;
384#endif
385
386 if (WARN_ONCE((unsigned long)rx->skb->data & 1,
387 "unaligned packet at 0x%p\n", rx->skb->data))
388 return;
389
Harvey Harrisona7767f92008-07-02 16:30:51 -0700390 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg38f37142008-01-29 17:07:43 +0100391 return;
392
Harvey Harrisona7767f92008-07-02 16:30:51 -0700393 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg5cf121c2008-02-25 16:27:43 +0100394 if (rx->flags & IEEE80211_RX_AMSDU)
Johannes Berg38f37142008-01-29 17:07:43 +0100395 hdrlen += ETH_HLEN;
Johannes Bergd1c3a372009-01-07 00:26:10 +0100396 WARN_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3,
397 "unaligned IP payload at 0x%p\n", rx->skb->data + hdrlen);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200398}
399
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200400
Johannes Berg571ecf62007-07-27 15:43:22 +0200401/* rx handlers */
402
Johannes Berg49461622008-06-30 15:10:45 +0200403static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100404ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200405{
406 struct ieee80211_local *local = rx->local;
407 struct sk_buff *skb = rx->skb;
408
Helmut Schaafbe9c422009-07-23 12:14:04 +0200409 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning)))
Johannes Bergf1d58c22009-06-17 13:13:00 +0200410 return ieee80211_scan_rx(rx->sdata, skb);
Zhu Yiece8edd2007-11-22 10:53:21 +0800411
Helmut Schaa142b9f52009-07-23 13:18:01 +0200412 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning) &&
413 (rx->flags & IEEE80211_RX_IN_SCAN))) {
Zhu Yiece8edd2007-11-22 10:53:21 +0800414 /* drop all the other packets during a software scan anyway */
Johannes Bergf1d58c22009-06-17 13:13:00 +0200415 if (ieee80211_scan_rx(rx->sdata, skb) != RX_QUEUED)
Zhu Yiece8edd2007-11-22 10:53:21 +0800416 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100417 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200418 }
419
Johannes Berg5cf121c2008-02-25 16:27:43 +0100420 if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200421 /* scanning finished during invoking of handlers */
422 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100423 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200424 }
425
Johannes Berg9ae54c82008-01-31 19:48:20 +0100426 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200427}
428
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200429
430static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
431{
432 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
433
434 if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
435 return 0;
436
437 return ieee80211_is_robust_mgmt_frame(hdr);
438}
439
440
441static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
442{
443 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
444
445 if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
446 return 0;
447
448 return ieee80211_is_robust_mgmt_frame(hdr);
449}
450
451
452/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
453static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
454{
455 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
456 struct ieee80211_mmie *mmie;
457
458 if (skb->len < 24 + sizeof(*mmie) ||
459 !is_multicast_ether_addr(hdr->da))
460 return -1;
461
462 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
463 return -1; /* not a robust management frame */
464
465 mmie = (struct ieee80211_mmie *)
466 (skb->data + skb->len - sizeof(*mmie));
467 if (mmie->element_id != WLAN_EID_MMIE ||
468 mmie->length != sizeof(*mmie) - 2)
469 return -1;
470
471 return le16_to_cpu(mmie->key_id);
472}
473
474
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100475static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100476ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100477{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700478 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
479 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700480 char *dev_addr = rx->dev->dev_addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100481
Harvey Harrisona7767f92008-07-02 16:30:51 -0700482 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700483 if (is_multicast_ether_addr(hdr->addr1)) {
484 if (ieee80211_has_tods(hdr->frame_control) ||
485 !ieee80211_has_fromds(hdr->frame_control))
486 return RX_DROP_MONITOR;
487 if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
488 return RX_DROP_MONITOR;
489 } else {
490 if (!ieee80211_has_a4(hdr->frame_control))
491 return RX_DROP_MONITOR;
492 if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
493 return RX_DROP_MONITOR;
494 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100495 }
496
497 /* If there is not an established peer link and this is not a peer link
498 * establisment frame, beacon or probe, drop the frame.
499 */
500
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800501 if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100502 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100503
Harvey Harrisona7767f92008-07-02 16:30:51 -0700504 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100505 return RX_DROP_MONITOR;
506
Harvey Harrisona7767f92008-07-02 16:30:51 -0700507 if (ieee80211_is_action(hdr->frame_control)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100508 mgmt = (struct ieee80211_mgmt *)hdr;
Rui Paulo09383932009-11-09 23:46:43 +0000509 if (mgmt->u.action.category != MESH_PLINK_CATEGORY)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100510 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100511 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100512 }
513
Harvey Harrisona7767f92008-07-02 16:30:51 -0700514 if (ieee80211_is_probe_req(hdr->frame_control) ||
515 ieee80211_is_probe_resp(hdr->frame_control) ||
516 ieee80211_is_beacon(hdr->frame_control))
517 return RX_CONTINUE;
518
519 return RX_DROP_MONITOR;
520
521 }
522
523#define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
524
525 if (ieee80211_is_data(hdr->frame_control) &&
526 is_multicast_ether_addr(hdr->addr1) &&
Javier Cardona3c5772a2009-08-10 12:15:48 -0700527 mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100528 return RX_DROP_MONITOR;
Johannes Berg902acc72008-02-23 15:17:19 +0100529#undef msh_h_get
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100530
Johannes Berg902acc72008-02-23 15:17:19 +0100531 return RX_CONTINUE;
532}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100533
534
Johannes Berg49461622008-06-30 15:10:45 +0200535static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100536ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200537{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700538 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +0200539
540 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
541 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
Harvey Harrisona7767f92008-07-02 16:30:51 -0700542 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100543 rx->sta->last_seq_ctrl[rx->queue] ==
Johannes Berg571ecf62007-07-27 15:43:22 +0200544 hdr->seq_ctrl)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100545 if (rx->flags & IEEE80211_RX_RA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200546 rx->local->dot11FrameDuplicateCount++;
547 rx->sta->num_duplicates++;
548 }
Johannes Berge4c26ad2008-01-31 19:48:21 +0100549 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200550 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100551 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +0200552 }
553
Johannes Berg571ecf62007-07-27 15:43:22 +0200554 if (unlikely(rx->skb->len < 16)) {
555 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100556 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200557 }
558
Johannes Berg571ecf62007-07-27 15:43:22 +0200559 /* Drop disallowed frame classes based on STA auth/assoc state;
560 * IEEE 802.11, Chap 5.5.
561 *
Johannes Bergccd7b362008-09-11 00:01:56 +0200562 * mac80211 filters only based on association state, i.e. it drops
563 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +0200564 * deauth/disassoc frames when needed. In addition, hostapd is
565 * responsible for filtering on both auth and assoc states.
566 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100567
Johannes Berg902acc72008-02-23 15:17:19 +0100568 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100569 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100570
Harvey Harrisona7767f92008-07-02 16:30:51 -0700571 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
572 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200573 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Johannes Berg07346f812008-05-03 01:02:02 +0200574 (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
Harvey Harrisona7767f92008-07-02 16:30:51 -0700575 if ((!ieee80211_has_fromds(hdr->frame_control) &&
576 !ieee80211_has_tods(hdr->frame_control) &&
577 ieee80211_is_data(hdr->frame_control)) ||
578 !(rx->flags & IEEE80211_RX_RA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200579 /* Drop IBSS frames and frames for other hosts
580 * silently. */
Johannes Berge4c26ad2008-01-31 19:48:21 +0100581 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200582 }
583
Johannes Berge4c26ad2008-01-31 19:48:21 +0100584 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200585 }
586
Johannes Berg9ae54c82008-01-31 19:48:20 +0100587 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +0200588}
589
590
Johannes Berg49461622008-06-30 15:10:45 +0200591static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100592ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200593{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700594 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400595 int keyidx;
596 int hdrlen;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100597 ieee80211_rx_result result = RX_DROP_UNUSABLE;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400598 struct ieee80211_key *stakey = NULL;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200599 int mmie_keyidx = -1;
Johannes Berg570bd532007-07-27 15:43:22 +0200600
Johannes Berg3017b802007-08-28 17:01:53 -0400601 /*
602 * Key selection 101
603 *
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200604 * There are four types of keys:
Johannes Berg3017b802007-08-28 17:01:53 -0400605 * - GTK (group keys)
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200606 * - IGTK (group keys for management frames)
Johannes Berg3017b802007-08-28 17:01:53 -0400607 * - PTK (pairwise keys)
608 * - STK (station-to-station pairwise keys)
609 *
610 * When selecting a key, we have to distinguish between multicast
611 * (including broadcast) and unicast frames, the latter can only
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200612 * use PTKs and STKs while the former always use GTKs and IGTKs.
613 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
614 * unicast frames can also use key indices like GTKs. Hence, if we
615 * don't have a PTK/STK we check the key index for a WEP key.
Johannes Berg3017b802007-08-28 17:01:53 -0400616 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400617 * Note that in a regular BSS, multicast frames are sent by the
618 * AP only, associated stations unicast the frame to the AP first
619 * which then multicasts it on their behalf.
620 *
Johannes Berg3017b802007-08-28 17:01:53 -0400621 * There is also a slight problem in IBSS mode: GTKs are negotiated
622 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400623 * The spec seems to expect that one negotiates the same key with
624 * every station but there's no such requirement; VLANs could be
625 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400626 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200627
Johannes Berg3017b802007-08-28 17:01:53 -0400628 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200629 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400630 * addressed to us nor a multicast frame.
631 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100632 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100633 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -0400634
Johannes Bergd4e46a32007-09-14 11:10:24 -0400635 if (rx->sta)
636 stakey = rcu_dereference(rx->sta->key);
637
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300638 if (!ieee80211_has_protected(hdr->frame_control))
639 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
640
Johannes Bergd4e46a32007-09-14 11:10:24 -0400641 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
642 rx->key = stakey;
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300643 /* Skip decryption if the frame is not protected. */
644 if (!ieee80211_has_protected(hdr->frame_control))
645 return RX_CONTINUE;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200646 } else if (mmie_keyidx >= 0) {
647 /* Broadcast/multicast robust management frame / BIP */
648 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
649 (rx->status->flag & RX_FLAG_IV_STRIPPED))
650 return RX_CONTINUE;
651
652 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
653 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
654 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
655 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300656 } else if (!ieee80211_has_protected(hdr->frame_control)) {
657 /*
658 * The frame was not protected, so skip decryption. However, we
659 * need to set rx->key if there is a key that could have been
660 * used so that the frame may be dropped if encryption would
661 * have been expected.
662 */
663 struct ieee80211_key *key = NULL;
664 if (ieee80211_is_mgmt(hdr->frame_control) &&
665 is_multicast_ether_addr(hdr->addr1) &&
666 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
667 rx->key = key;
668 else if ((key = rcu_dereference(rx->sdata->default_key)))
669 rx->key = key;
670 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200671 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400672 /*
673 * The device doesn't give us the IV so we won't be
674 * able to look up the key. That's ok though, we
675 * don't need to decrypt the frame, we just won't
676 * be able to keep statistics accurate.
677 * Except for key threshold notifications, should
678 * we somehow allow the driver to tell us which key
679 * the hardware used if this flag is set?
680 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100681 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
682 (rx->status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100683 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200684
Harvey Harrisona7767f92008-07-02 16:30:51 -0700685 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +0200686
Johannes Berg3017b802007-08-28 17:01:53 -0400687 if (rx->skb->len < 8 + hdrlen)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100688 return RX_DROP_UNUSABLE; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200689
Johannes Berg3017b802007-08-28 17:01:53 -0400690 /*
691 * no need to call ieee80211_wep_get_keyidx,
692 * it verifies a bunch of things we've done already
693 */
694 keyidx = rx->skb->data[hdrlen + 3] >> 6;
695
Johannes Bergd4e46a32007-09-14 11:10:24 -0400696 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400697
698 /*
699 * RSNA-protected unicast frames should always be sent with
700 * pairwise or station-to-station keys, but for WEP we allow
701 * using a key index as well.
702 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400703 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400704 !is_multicast_ether_addr(hdr->addr1))
705 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200706 }
707
Johannes Berg3017b802007-08-28 17:01:53 -0400708 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200709 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400710 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200711 } else {
Johannes Berge4c26ad2008-01-31 19:48:21 +0100712 return RX_DROP_MONITOR;
Johannes Berg70f08762007-09-26 17:53:14 +0200713 }
714
Johannes Berg1990af82007-09-26 17:53:15 +0200715 /* Check for weak IVs if possible */
716 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
Harvey Harrisona7767f92008-07-02 16:30:51 -0700717 ieee80211_is_data(hdr->frame_control) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100718 (!(rx->status->flag & RX_FLAG_IV_STRIPPED) ||
719 !(rx->status->flag & RX_FLAG_DECRYPTED)) &&
Johannes Berg1990af82007-09-26 17:53:15 +0200720 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
721 rx->sta->wep_weak_iv_count++;
722
Johannes Berg70f08762007-09-26 17:53:14 +0200723 switch (rx->key->conf.alg) {
724 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200725 result = ieee80211_crypto_wep_decrypt(rx);
726 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200727 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200728 result = ieee80211_crypto_tkip_decrypt(rx);
729 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200730 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200731 result = ieee80211_crypto_ccmp_decrypt(rx);
732 break;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200733 case ALG_AES_CMAC:
734 result = ieee80211_crypto_aes_cmac_decrypt(rx);
735 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200736 }
737
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200738 /* either the frame has been decrypted or will be dropped */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100739 rx->status->flag |= RX_FLAG_DECRYPTED;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200740
741 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200742}
743
Kalle Valo572e0012009-02-10 17:09:31 +0200744static ieee80211_rx_result debug_noinline
745ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
746{
747 struct ieee80211_local *local;
748 struct ieee80211_hdr *hdr;
749 struct sk_buff *skb;
750
751 local = rx->local;
752 skb = rx->skb;
753 hdr = (struct ieee80211_hdr *) skb->data;
754
755 if (!local->pspolling)
756 return RX_CONTINUE;
757
758 if (!ieee80211_has_fromds(hdr->frame_control))
759 /* this is not from AP */
760 return RX_CONTINUE;
761
762 if (!ieee80211_is_data(hdr->frame_control))
763 return RX_CONTINUE;
764
765 if (!ieee80211_has_moredata(hdr->frame_control)) {
766 /* AP has no more frames buffered for us */
767 local->pspolling = false;
768 return RX_CONTINUE;
769 }
770
771 /* more data bit is set, let's request a new frame from the AP */
772 ieee80211_send_pspoll(local, rx->sdata);
773
774 return RX_CONTINUE;
775}
776
Johannes Berg133b8222008-09-16 14:18:59 +0200777static void ap_sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +0200778{
Johannes Berg133b8222008-09-16 14:18:59 +0200779 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +0100780 struct ieee80211_local *local = sdata->local;
Joe Perches0795af52007-10-03 17:59:30 -0700781
Johannes Berg3e122be2008-07-09 14:40:34 +0200782 atomic_inc(&sdata->bss->num_sta_ps);
Johannes Bergaf818582009-11-06 11:35:50 +0100783 set_sta_flags(sta, WLAN_STA_PS_STA);
Johannes Berg24487982009-04-23 18:52:52 +0200784 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Berg571ecf62007-07-27 15:43:22 +0200785#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700786 printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
787 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200788#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
789}
790
Johannes Bergff9458d2009-10-30 12:56:02 +0100791static void ap_sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +0200792{
Johannes Berg133b8222008-09-16 14:18:59 +0200793 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +0200794
Johannes Berg3e122be2008-07-09 14:40:34 +0200795 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg004c8722008-02-20 11:21:35 +0100796
Johannes Bergaf818582009-11-06 11:35:50 +0100797 clear_sta_flags(sta, WLAN_STA_PS_STA);
Johannes Berg004c8722008-02-20 11:21:35 +0100798
Johannes Berg571ecf62007-07-27 15:43:22 +0200799#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700800 printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
801 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200802#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg004c8722008-02-20 11:21:35 +0100803
Johannes Bergaf818582009-11-06 11:35:50 +0100804 if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200805#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Bergaf818582009-11-06 11:35:50 +0100806 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
807 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200808#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Bergaf818582009-11-06 11:35:50 +0100809 return;
810 }
811
812 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +0200813}
814
Johannes Berg49461622008-06-30 15:10:45 +0200815static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100816ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200817{
818 struct sta_info *sta = rx->sta;
Harvey Harrisona7767f92008-07-02 16:30:51 -0700819 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +0200820
821 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100822 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200823
Johannes Bergb291ba12009-07-10 15:29:03 +0200824 /*
825 * Update last_rx only for IBSS packets which are for the current
826 * BSSID to avoid keeping the current IBSS network alive in cases
827 * where other STAs start using different BSSID.
828 */
Johannes Berg05c914f2008-09-11 00:01:58 +0200829 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +0200830 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +0200831 NL80211_IFTYPE_ADHOC);
Johannes Berg46900292009-02-15 12:44:28 +0100832 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
Johannes Berg571ecf62007-07-27 15:43:22 +0200833 sta->last_rx = jiffies;
Johannes Bergb291ba12009-07-10 15:29:03 +0200834 } else if (!is_multicast_ether_addr(hdr->addr1)) {
835 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100836 * Mesh beacons will update last_rx when if they are found to
837 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +0200838 */
Johannes Bergb291ba12009-07-10 15:29:03 +0200839 sta->last_rx = jiffies;
Johannes Berg571ecf62007-07-27 15:43:22 +0200840 }
841
Johannes Berg5cf121c2008-02-25 16:27:43 +0100842 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100843 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200844
Kalle Valo3cf335d2009-03-22 21:57:06 +0200845 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
846 ieee80211_sta_rx_notify(rx->sdata, hdr);
847
Johannes Berg571ecf62007-07-27 15:43:22 +0200848 sta->rx_fragments++;
849 sta->rx_bytes += rx->skb->len;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100850 sta->last_signal = rx->status->signal;
851 sta->last_noise = rx->status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200852
Johannes Berg72eaa432008-11-26 15:02:58 +0100853 /*
854 * Change STA power saving mode only at the end of a frame
855 * exchange sequence.
856 */
Johannes Berg3e122be2008-07-09 14:40:34 +0200857 if (!ieee80211_has_morefrags(hdr->frame_control) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200858 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
859 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergaf818582009-11-06 11:35:50 +0100860 if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
Johannes Berg72eaa432008-11-26 15:02:58 +0100861 /*
862 * Ignore doze->wake transitions that are
863 * indicated by non-data frames, the standard
864 * is unclear here, but for example going to
865 * PS mode and then scanning would cause a
866 * doze->wake transition for the probe request,
867 * and that is clearly undesirable.
868 */
869 if (ieee80211_is_data(hdr->frame_control) &&
870 !ieee80211_has_pm(hdr->frame_control))
Johannes Bergff9458d2009-10-30 12:56:02 +0100871 ap_sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +0100872 } else {
873 if (ieee80211_has_pm(hdr->frame_control))
874 ap_sta_ps_start(sta);
875 }
Johannes Berg571ecf62007-07-27 15:43:22 +0200876 }
877
Johannes Berg22403de2009-10-30 12:55:03 +0100878 /*
879 * Drop (qos-)data::nullfunc frames silently, since they
880 * are used only to control station power saving mode.
881 */
882 if (ieee80211_is_nullfunc(hdr->frame_control) ||
883 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200884 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Johannes Berg22403de2009-10-30 12:55:03 +0100885 /*
886 * Update counter and free packet here to avoid
887 * counting this as a dropped packed.
888 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200889 sta->rx_packets++;
890 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100891 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200892 }
893
Johannes Berg9ae54c82008-01-31 19:48:20 +0100894 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200895} /* ieee80211_rx_h_sta_process */
896
Johannes Berg571ecf62007-07-27 15:43:22 +0200897static inline struct ieee80211_fragment_entry *
898ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
899 unsigned int frag, unsigned int seq, int rx_queue,
900 struct sk_buff **skb)
901{
902 struct ieee80211_fragment_entry *entry;
903 int idx;
904
905 idx = sdata->fragment_next;
906 entry = &sdata->fragments[sdata->fragment_next++];
907 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
908 sdata->fragment_next = 0;
909
910 if (!skb_queue_empty(&entry->skb_list)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200911#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg571ecf62007-07-27 15:43:22 +0200912 struct ieee80211_hdr *hdr =
913 (struct ieee80211_hdr *) entry->skb_list.next->data;
914 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
915 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Johannes Berg0c68ae262008-10-27 15:56:10 -0700916 "addr1=%pM addr2=%pM\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200917 sdata->dev->name, idx,
918 jiffies - entry->first_frag_time, entry->seq,
Johannes Berg0c68ae262008-10-27 15:56:10 -0700919 entry->last_frag, hdr->addr1, hdr->addr2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200920#endif
Johannes Berg571ecf62007-07-27 15:43:22 +0200921 __skb_queue_purge(&entry->skb_list);
922 }
923
924 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
925 *skb = NULL;
926 entry->first_frag_time = jiffies;
927 entry->seq = seq;
928 entry->rx_queue = rx_queue;
929 entry->last_frag = frag;
930 entry->ccmp = 0;
931 entry->extra_len = 0;
932
933 return entry;
934}
935
936static inline struct ieee80211_fragment_entry *
937ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700938 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +0200939 int rx_queue, struct ieee80211_hdr *hdr)
940{
941 struct ieee80211_fragment_entry *entry;
942 int i, idx;
943
944 idx = sdata->fragment_next;
945 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
946 struct ieee80211_hdr *f_hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200947
948 idx--;
949 if (idx < 0)
950 idx = IEEE80211_FRAGMENT_MAX - 1;
951
952 entry = &sdata->fragments[idx];
953 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
954 entry->rx_queue != rx_queue ||
955 entry->last_frag + 1 != frag)
956 continue;
957
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700958 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
Johannes Berg571ecf62007-07-27 15:43:22 +0200959
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700960 /*
961 * Check ftype and addresses are equal, else check next fragment
962 */
963 if (((hdr->frame_control ^ f_hdr->frame_control) &
964 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200965 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
966 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
967 continue;
968
S.Çağlar Onurab466232008-02-14 17:36:47 +0200969 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200970 __skb_queue_purge(&entry->skb_list);
971 continue;
972 }
973 return entry;
974 }
975
976 return NULL;
977}
978
Johannes Berg49461622008-06-30 15:10:45 +0200979static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100980ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200981{
982 struct ieee80211_hdr *hdr;
983 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -0700984 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +0200985 unsigned int frag, seq;
986 struct ieee80211_fragment_entry *entry;
987 struct sk_buff *skb;
988
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700989 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -0700990 fc = hdr->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +0200991 sc = le16_to_cpu(hdr->seq_ctrl);
992 frag = sc & IEEE80211_SCTL_FRAG;
993
Harvey Harrison358c8d92008-07-15 18:44:13 -0700994 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200995 (rx->skb)->len < 24 ||
996 is_multicast_ether_addr(hdr->addr1))) {
997 /* not fragmented */
998 goto out;
999 }
1000 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1001
1002 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1003
1004 if (frag == 0) {
1005 /* This is the first fragment of a new frame. */
1006 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001007 rx->queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -04001008 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07001009 ieee80211_has_protected(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001010 /* Store CCMP PN so that we can verify that the next
1011 * fragment has a sequential PN value. */
1012 entry->ccmp = 1;
1013 memcpy(entry->last_pn,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001014 rx->key->u.ccmp.rx_pn[rx->queue],
Johannes Berg571ecf62007-07-27 15:43:22 +02001015 CCMP_PN_LEN);
1016 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01001017 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001018 }
1019
1020 /* This is a fragment for a frame that should already be pending in
1021 * fragment cache. Add this fragment to the end of the pending entry.
1022 */
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001023 entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001024 if (!entry) {
1025 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001026 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001027 }
1028
1029 /* Verify that MPDUs within one MSDU have sequential PN values.
1030 * (IEEE 802.11i, 8.3.3.4.5) */
1031 if (entry->ccmp) {
1032 int i;
1033 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -04001034 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001035 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001036 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1037 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1038 pn[i]++;
1039 if (pn[i])
1040 break;
1041 }
Johannes Berg5cf121c2008-02-25 16:27:43 +01001042 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001043 if (memcmp(pn, rpn, CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001044 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001045 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1046 }
1047
Harvey Harrison358c8d92008-07-15 18:44:13 -07001048 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02001049 __skb_queue_tail(&entry->skb_list, rx->skb);
1050 entry->last_frag = frag;
1051 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001052 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001053 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001054 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001055 }
1056
1057 rx->skb = __skb_dequeue(&entry->skb_list);
1058 if (skb_tailroom(rx->skb) < entry->extra_len) {
1059 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1060 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1061 GFP_ATOMIC))) {
1062 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1063 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001064 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001065 }
1066 }
1067 while ((skb = __skb_dequeue(&entry->skb_list))) {
1068 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1069 dev_kfree_skb(skb);
1070 }
1071
1072 /* Complete frame has been reassembled - process it now */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001073 rx->flags |= IEEE80211_RX_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001074
1075 out:
1076 if (rx->sta)
1077 rx->sta->rx_packets++;
1078 if (is_multicast_ether_addr(hdr->addr1))
1079 rx->local->dot11MulticastReceivedFrameCount++;
1080 else
1081 ieee80211_led_rx(rx->local);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001082 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001083}
1084
Johannes Berg49461622008-06-30 15:10:45 +02001085static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001086ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001087{
Johannes Bergaf818582009-11-06 11:35:50 +01001088 struct ieee80211_sub_if_data *sdata = rx->sdata;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001089 __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02001090
Harvey Harrison358c8d92008-07-15 18:44:13 -07001091 if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
Johannes Berg5cf121c2008-02-25 16:27:43 +01001092 !(rx->flags & IEEE80211_RX_RA_MATCH)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001093 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001094
Johannes Berg05c914f2008-09-11 00:01:58 +02001095 if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1096 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001097 return RX_DROP_UNUSABLE;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +02001098
Johannes Bergaf818582009-11-06 11:35:50 +01001099 if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
1100 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1101 else
1102 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
Johannes Berg571ecf62007-07-27 15:43:22 +02001103
Johannes Berg9ae54c82008-01-31 19:48:20 +01001104 /* Free PS Poll skb here instead of returning RX_DROP that would
Johannes Berg571ecf62007-07-27 15:43:22 +02001105 * count as an dropped frame. */
1106 dev_kfree_skb(rx->skb);
1107
Johannes Berg9ae54c82008-01-31 19:48:20 +01001108 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001109}
1110
Johannes Berg49461622008-06-30 15:10:45 +02001111static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001112ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +02001113{
Johannes Berg6e0d1142007-07-27 15:43:22 +02001114 u8 *data = rx->skb->data;
Harvey Harrison238f74a2008-07-02 11:05:34 -07001115 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001116
Harvey Harrison238f74a2008-07-02 11:05:34 -07001117 if (!ieee80211_is_data_qos(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001118 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001119
1120 /* remove the qos control field, update frame type and meta-data */
Harvey Harrison238f74a2008-07-02 11:05:34 -07001121 memmove(data + IEEE80211_QOS_CTL_LEN, data,
1122 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1123 hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
Johannes Berg6e0d1142007-07-27 15:43:22 +02001124 /* change frame type to non QOS */
Harvey Harrison238f74a2008-07-02 11:05:34 -07001125 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Berg6e0d1142007-07-27 15:43:22 +02001126
Johannes Berg9ae54c82008-01-31 19:48:20 +01001127 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001128}
1129
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001130static int
Johannes Berg5cf121c2008-02-25 16:27:43 +01001131ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001132{
Johannes Berg07346f812008-05-03 01:02:02 +02001133 if (unlikely(!rx->sta ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001134 !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001135 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001136
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001137 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001138}
1139
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001140static int
Harvey Harrison358c8d92008-07-15 18:44:13 -07001141ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02001142{
Johannes Berg3017b802007-08-28 17:01:53 -04001143 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001144 * Pass through unencrypted frames if the hardware has
1145 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001146 */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001147 if (rx->status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001148 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001149
1150 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001151 if (unlikely(!ieee80211_has_protected(fc) &&
1152 !ieee80211_is_nullfunc(fc) &&
Jouni Malinenf2ca3ea42009-05-08 12:36:03 +03001153 ieee80211_is_data(fc) &&
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001154 (rx->key || rx->sdata->drop_unencrypted)))
1155 return -EACCES;
Jouni Malinenf2ca3ea42009-05-08 12:36:03 +03001156 if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1157 if (unlikely(ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1158 rx->key))
1159 return -EACCES;
1160 /* BIP does not use Protected field, so need to check MMIE */
1161 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb)
1162 && ieee80211_get_mmie_keyidx(rx->skb) < 0 &&
1163 rx->key))
1164 return -EACCES;
1165 /*
1166 * When using MFP, Action frames are not allowed prior to
1167 * having configured keys.
1168 */
1169 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1170 ieee80211_is_robust_mgmt_frame(
1171 (struct ieee80211_hdr *) rx->skb->data)))
1172 return -EACCES;
1173 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02001174
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001175 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001176}
1177
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001178static int
Zhu Yie31a16d2009-05-21 21:47:03 +08001179__ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001180{
1181 struct net_device *dev = rx->dev;
Johannes Berg571ecf62007-07-27 15:43:22 +02001182 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkauf14543e2009-11-10 20:10:05 +01001183 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1184
1185 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->use_4addr &&
1186 ieee80211_has_a4(hdr->frame_control))
1187 return -1;
1188 if (sdata->use_4addr && is_multicast_ether_addr(hdr->addr1))
1189 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001190
Zhu Yie31a16d2009-05-21 21:47:03 +08001191 return ieee80211_data_to_8023(rx->skb, dev->dev_addr, sdata->vif.type);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001192}
Johannes Berg571ecf62007-07-27 15:43:22 +02001193
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001194/*
1195 * requires that rx->skb is a frame with ethernet header
1196 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001197static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001198{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301199 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001200 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1201 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1202
1203 /*
1204 * Allow EAPOL frames to us/the PAE group address regardless
1205 * of whether the frame was encrypted or not.
1206 */
1207 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1208 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1209 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1210 return true;
1211
1212 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07001213 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001214 return false;
1215
1216 return true;
1217}
1218
1219/*
1220 * requires that rx->skb is a frame with ethernet header
1221 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001222static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01001223ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001224{
1225 struct net_device *dev = rx->dev;
1226 struct ieee80211_local *local = rx->local;
1227 struct sk_buff *skb, *xmit_skb;
1228 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001229 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1230 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001231
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001232 skb = rx->skb;
1233 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001234
Johannes Berg05c914f2008-09-11 00:01:58 +02001235 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1236 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02001237 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Felix Fietkauf501dba2009-11-11 13:17:36 +01001238 (rx->flags & IEEE80211_RX_RA_MATCH) && !rx->sdata->use_4addr) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001239 if (is_multicast_ether_addr(ehdr->h_dest)) {
1240 /*
1241 * send multicast frames both to higher layers in
1242 * local net stack and back to the wireless medium
1243 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001244 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1245 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001246 printk(KERN_DEBUG "%s: failed to clone "
1247 "multicast frame\n", dev->name);
1248 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001249 dsta = sta_info_get(local, skb->data);
Johannes Bergd0709a62008-02-25 16:27:46 +01001250 if (dsta && dsta->sdata->dev == dev) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001251 /*
1252 * The destination station is associated to
1253 * this AP (in this VLAN), so send the frame
1254 * directly to it and do not pass it to local
1255 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001256 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001257 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001258 skb = NULL;
1259 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001260 }
1261 }
1262
1263 if (skb) {
Johannes Bergd1c3a372009-01-07 00:26:10 +01001264 int align __maybe_unused;
1265
1266#if defined(CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT) || !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1267 /*
1268 * 'align' will only take the values 0 or 2 here
1269 * since all frames are required to be aligned
1270 * to 2-byte boundaries when being passed to
1271 * mac80211. That also explains the __skb_push()
1272 * below.
1273 */
matthieu castetdacb6f12009-06-04 22:16:18 +02001274 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01001275 if (align) {
1276 if (WARN_ON(skb_headroom(skb) < 3)) {
1277 dev_kfree_skb(skb);
1278 skb = NULL;
1279 } else {
1280 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07001281 size_t len = skb_headlen(skb);
1282 skb->data -= align;
1283 memmove(skb->data, data, len);
1284 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01001285 }
1286 }
1287#endif
1288
1289 if (skb) {
1290 /* deliver to local stack */
1291 skb->protocol = eth_type_trans(skb, dev);
1292 memset(skb->cb, 0, sizeof(skb->cb));
1293 netif_rx(skb);
1294 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001295 }
1296
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001297 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001298 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001299 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001300 skb_reset_network_header(xmit_skb);
1301 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001302 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001303 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001304}
1305
Johannes Berg49461622008-06-30 15:10:45 +02001306static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001307ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001308{
1309 struct net_device *dev = rx->dev;
1310 struct ieee80211_local *local = rx->local;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001311 u16 ethertype;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001312 u8 *payload;
1313 struct sk_buff *skb = rx->skb, *frame = NULL;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001314 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1315 __le16 fc = hdr->frame_control;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001316 const struct ethhdr *eth;
1317 int remaining, err;
1318 u8 dst[ETH_ALEN];
1319 u8 src[ETH_ALEN];
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001320
Harvey Harrison358c8d92008-07-15 18:44:13 -07001321 if (unlikely(!ieee80211_is_data(fc)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001322 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001323
Harvey Harrison358c8d92008-07-15 18:44:13 -07001324 if (unlikely(!ieee80211_is_data_present(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001325 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001326
Johannes Berg5cf121c2008-02-25 16:27:43 +01001327 if (!(rx->flags & IEEE80211_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001328 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001329
Zhu Yie31a16d2009-05-21 21:47:03 +08001330 err = __ieee80211_data_to_8023(rx);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001331 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001332 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001333
1334 skb->dev = dev;
1335
1336 dev->stats.rx_packets++;
1337 dev->stats.rx_bytes += skb->len;
1338
1339 /* skip the wrapping header */
1340 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1341 if (!eth)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001342 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001343
1344 while (skb != frame) {
1345 u8 padding;
1346 __be16 len = eth->h_proto;
1347 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1348
1349 remaining = skb->len;
1350 memcpy(dst, eth->h_dest, ETH_ALEN);
1351 memcpy(src, eth->h_source, ETH_ALEN);
1352
1353 padding = ((4 - subframe_len) & 0x3);
1354 /* the last MSDU has no padding */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001355 if (subframe_len > remaining)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001356 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001357
1358 skb_pull(skb, sizeof(struct ethhdr));
1359 /* if last subframe reuse skb */
1360 if (remaining <= subframe_len + padding)
1361 frame = skb;
1362 else {
Johannes Bergd1c3a372009-01-07 00:26:10 +01001363 /*
1364 * Allocate and reserve two bytes more for payload
1365 * alignment since sizeof(struct ethhdr) is 14.
1366 */
1367 frame = dev_alloc_skb(
1368 ALIGN(local->hw.extra_tx_headroom, 4) +
1369 subframe_len + 2);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001370
1371 if (frame == NULL)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001372 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001373
Johannes Bergd1c3a372009-01-07 00:26:10 +01001374 skb_reserve(frame,
1375 ALIGN(local->hw.extra_tx_headroom, 4) +
1376 sizeof(struct ethhdr) + 2);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001377 memcpy(skb_put(frame, ntohs(len)), skb->data,
1378 ntohs(len));
1379
1380 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1381 padding);
1382 if (!eth) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001383 dev_kfree_skb(frame);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001384 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001385 }
1386 }
1387
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001388 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001389 frame->dev = dev;
1390 frame->priority = skb->priority;
1391 rx->skb = frame;
1392
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001393 payload = frame->data;
1394 ethertype = (payload[6] << 8) | payload[7];
1395
1396 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001397 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1398 compare_ether_addr(payload,
1399 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001400 /* remove RFC1042 or Bridge-Tunnel
1401 * encapsulation and replace EtherType */
1402 skb_pull(frame, 6);
1403 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1404 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1405 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001406 memcpy(skb_push(frame, sizeof(__be16)),
1407 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001408 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1409 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1410 }
1411
Harvey Harrison358c8d92008-07-15 18:44:13 -07001412 if (!ieee80211_frame_allowed(rx, fc)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001413 if (skb == frame) /* last frame */
Johannes Berge4c26ad2008-01-31 19:48:21 +01001414 return RX_DROP_UNUSABLE;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001415 dev_kfree_skb(frame);
1416 continue;
1417 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001418
1419 ieee80211_deliver_skb(rx);
1420 }
1421
Johannes Berg9ae54c82008-01-31 19:48:20 +01001422 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001423}
1424
Ingo Molnarbf94e172008-10-12 23:51:38 -07001425#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02001426static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001427ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1428{
1429 struct ieee80211_hdr *hdr;
1430 struct ieee80211s_hdr *mesh_hdr;
1431 unsigned int hdrlen;
1432 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001433 struct ieee80211_local *local = rx->local;
Javier Cardona249b4052009-07-07 10:55:03 -07001434 struct ieee80211_sub_if_data *sdata;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001435
1436 hdr = (struct ieee80211_hdr *) skb->data;
1437 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1438 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
Javier Cardona249b4052009-07-07 10:55:03 -07001439 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001440
1441 if (!ieee80211_is_data(hdr->frame_control))
1442 return RX_CONTINUE;
1443
1444 if (!mesh_hdr->ttl)
1445 /* illegal frame */
1446 return RX_DROP_MONITOR;
1447
Javier Cardona43b7b312009-10-15 18:10:51 -07001448 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08001449 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07001450 char *proxied_addr;
1451 char *mpp_addr;
1452
1453 if (is_multicast_ether_addr(hdr->addr1)) {
1454 mpp_addr = hdr->addr3;
1455 proxied_addr = mesh_hdr->eaddr1;
1456 } else {
1457 mpp_addr = hdr->addr4;
1458 proxied_addr = mesh_hdr->eaddr2;
1459 }
YanBo79617de2008-09-22 13:30:32 +08001460
YanBo79617de2008-09-22 13:30:32 +08001461 rcu_read_lock();
Javier Cardona43b7b312009-10-15 18:10:51 -07001462 mppath = mpp_path_lookup(proxied_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001463 if (!mppath) {
Javier Cardona43b7b312009-10-15 18:10:51 -07001464 mpp_path_add(proxied_addr, mpp_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001465 } else {
1466 spin_lock_bh(&mppath->state_lock);
1467 mppath->exp_time = jiffies;
Javier Cardona43b7b312009-10-15 18:10:51 -07001468 if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1469 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08001470 spin_unlock_bh(&mppath->state_lock);
1471 }
1472 rcu_read_unlock();
1473 }
1474
Javier Cardona3c5772a2009-08-10 12:15:48 -07001475 /* Frame has reached destination. Don't forward */
1476 if (!is_multicast_ether_addr(hdr->addr1) &&
1477 compare_ether_addr(rx->dev->dev_addr, hdr->addr3) == 0)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001478 return RX_CONTINUE;
1479
1480 mesh_hdr->ttl--;
1481
1482 if (rx->flags & IEEE80211_RX_RA_MATCH) {
1483 if (!mesh_hdr->ttl)
Johannes Berg472dbc42008-09-11 00:01:49 +02001484 IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001485 dropped_frames_ttl);
1486 else {
1487 struct ieee80211_hdr *fwd_hdr;
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001488 struct ieee80211_tx_info *info;
1489
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001490 fwd_skb = skb_copy(skb, GFP_ATOMIC);
1491
1492 if (!fwd_skb && net_ratelimit())
1493 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
1494 rx->dev->name);
1495
1496 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001497 memcpy(fwd_hdr->addr2, rx->dev->dev_addr, ETH_ALEN);
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001498 info = IEEE80211_SKB_CB(fwd_skb);
1499 memset(info, 0, sizeof(*info));
1500 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001501 info->control.vif = &rx->sdata->vif;
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001502 ieee80211_select_queue(local, fwd_skb);
Daniel Walkerc8a61a72009-08-18 10:59:00 -07001503 if (is_multicast_ether_addr(fwd_hdr->addr1))
1504 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1505 fwded_mcast);
1506 else {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001507 int err;
1508 /*
1509 * Save TA to addr1 to send TA a path error if a
1510 * suitable next hop is not found
1511 */
1512 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
Javier Cardona249b4052009-07-07 10:55:03 -07001513 ETH_ALEN);
Javier Cardona3c5772a2009-08-10 12:15:48 -07001514 err = mesh_nexthop_lookup(fwd_skb, sdata);
Javier Cardona249b4052009-07-07 10:55:03 -07001515 /* Failed to immediately resolve next hop:
1516 * fwded frame was dropped or will be added
1517 * later to the pending skb queue. */
1518 if (err)
1519 return RX_DROP_MONITOR;
Daniel Walkerc8a61a72009-08-18 10:59:00 -07001520
1521 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1522 fwded_unicast);
Javier Cardona249b4052009-07-07 10:55:03 -07001523 }
1524 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1525 fwded_frames);
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001526 ieee80211_add_pending_skb(local, fwd_skb);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001527 }
1528 }
1529
Javier Cardona3c5772a2009-08-10 12:15:48 -07001530 if (is_multicast_ether_addr(hdr->addr1) ||
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001531 rx->dev->flags & IFF_PROMISC)
1532 return RX_CONTINUE;
1533 else
1534 return RX_DROP_MONITOR;
1535}
Ingo Molnarbf94e172008-10-12 23:51:38 -07001536#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001537
1538static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001539ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001540{
1541 struct net_device *dev = rx->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001542 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkauf14543e2009-11-10 20:10:05 +01001543 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Harvey Harrison358c8d92008-07-15 18:44:13 -07001544 __le16 fc = hdr->frame_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001545 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001546
Harvey Harrison358c8d92008-07-15 18:44:13 -07001547 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001548 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001549
Harvey Harrison358c8d92008-07-15 18:44:13 -07001550 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001551 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001552
Felix Fietkauf14543e2009-11-10 20:10:05 +01001553 /*
1554 * Allow the cooked monitor interface of an AP to see 4-addr frames so
1555 * that a 4-addr station can be detected and moved into a separate VLAN
1556 */
1557 if (ieee80211_has_a4(hdr->frame_control) &&
1558 sdata->vif.type == NL80211_IFTYPE_AP)
1559 return RX_DROP_MONITOR;
1560
Zhu Yie31a16d2009-05-21 21:47:03 +08001561 err = __ieee80211_data_to_8023(rx);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001562 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001563 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001564
Harvey Harrison358c8d92008-07-15 18:44:13 -07001565 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001566 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001567
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001568 rx->skb->dev = dev;
1569
1570 dev->stats.rx_packets++;
1571 dev->stats.rx_bytes += rx->skb->len;
1572
1573 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001574
Johannes Berg9ae54c82008-01-31 19:48:20 +01001575 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001576}
1577
Johannes Berg49461622008-06-30 15:10:45 +02001578static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001579ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
Ron Rindjunsky71364712007-12-25 17:00:36 +02001580{
1581 struct ieee80211_local *local = rx->local;
1582 struct ieee80211_hw *hw = &local->hw;
1583 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001584 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001585 struct tid_ampdu_rx *tid_agg_rx;
1586 u16 start_seq_num;
1587 u16 tid;
1588
Harvey Harrisona7767f92008-07-02 16:30:51 -07001589 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001590 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001591
Harvey Harrisona7767f92008-07-02 16:30:51 -07001592 if (ieee80211_is_back_req(bar->frame_control)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001593 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01001594 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001595 tid = le16_to_cpu(bar->control) >> 12;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001596 if (rx->sta->ampdu_mlme.tid_state_rx[tid]
1597 != HT_AGG_STATE_OPERATIONAL)
Johannes Berga02ae752009-11-16 12:00:40 +01001598 return RX_DROP_MONITOR;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001599 tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
Ron Rindjunsky71364712007-12-25 17:00:36 +02001600
1601 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1602
1603 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01001604 if (tid_agg_rx->timeout)
1605 mod_timer(&tid_agg_rx->session_timer,
1606 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02001607
Johannes Berga02ae752009-11-16 12:00:40 +01001608 /* release stored frames up to start of BAR */
1609 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
1610 kfree_skb(skb);
1611 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001612 }
1613
Johannes Berg9ae54c82008-01-31 19:48:20 +01001614 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001615}
1616
Jouni Malinenf4f727a2009-01-10 11:46:53 +02001617static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
1618 struct ieee80211_mgmt *mgmt,
1619 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02001620{
1621 struct ieee80211_local *local = sdata->local;
1622 struct sk_buff *skb;
1623 struct ieee80211_mgmt *resp;
1624
1625 if (compare_ether_addr(mgmt->da, sdata->dev->dev_addr) != 0) {
1626 /* Not to own unicast address */
1627 return;
1628 }
1629
Johannes Berg46900292009-02-15 12:44:28 +01001630 if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
1631 compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02001632 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02001633 return;
1634 }
1635
1636 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
1637 /* Too short SA Query request frame */
1638 return;
1639 }
1640
1641 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
1642 if (skb == NULL)
1643 return;
1644
1645 skb_reserve(skb, local->hw.extra_tx_headroom);
1646 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
1647 memset(resp, 0, 24);
1648 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1649 memcpy(resp->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01001650 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02001651 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1652 IEEE80211_STYPE_ACTION);
1653 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
1654 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
1655 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
1656 memcpy(resp->u.action.u.sa_query.trans_id,
1657 mgmt->u.action.u.sa_query.trans_id,
1658 WLAN_SA_QUERY_TR_ID_LEN);
1659
1660 ieee80211_tx_skb(sdata, skb, 1);
1661}
1662
Johannes Berg49461622008-06-30 15:10:45 +02001663static ieee80211_rx_result debug_noinline
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001664ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
1665{
1666 struct ieee80211_local *local = rx->local;
1667 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1668 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1669 int len = rx->skb->len;
1670
1671 if (!ieee80211_is_action(mgmt->frame_control))
1672 return RX_CONTINUE;
1673
1674 if (!rx->sta)
1675 return RX_DROP_MONITOR;
1676
1677 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1678 return RX_DROP_MONITOR;
1679
Jouni Malinen97ebe122009-01-08 13:32:08 +02001680 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1681 return RX_DROP_MONITOR;
1682
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001683 /* all categories we currently handle have action_code */
1684 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
1685 return RX_DROP_MONITOR;
1686
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001687 switch (mgmt->u.action.category) {
1688 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01001689 /*
1690 * The aggregation code is not prepared to handle
1691 * anything but STA/AP due to the BSSID handling;
1692 * IBSS could work in the code but isn't supported
1693 * by drivers or the standard.
1694 */
1695 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1696 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1697 sdata->vif.type != NL80211_IFTYPE_AP)
1698 return RX_DROP_MONITOR;
1699
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001700 switch (mgmt->u.action.u.addba_req.action_code) {
1701 case WLAN_ACTION_ADDBA_REQ:
1702 if (len < (IEEE80211_MIN_ACTION_SIZE +
1703 sizeof(mgmt->u.action.u.addba_req)))
1704 return RX_DROP_MONITOR;
1705 ieee80211_process_addba_request(local, rx->sta, mgmt, len);
1706 break;
1707 case WLAN_ACTION_ADDBA_RESP:
1708 if (len < (IEEE80211_MIN_ACTION_SIZE +
1709 sizeof(mgmt->u.action.u.addba_resp)))
1710 return RX_DROP_MONITOR;
1711 ieee80211_process_addba_resp(local, rx->sta, mgmt, len);
1712 break;
1713 case WLAN_ACTION_DELBA:
1714 if (len < (IEEE80211_MIN_ACTION_SIZE +
1715 sizeof(mgmt->u.action.u.delba)))
1716 return RX_DROP_MONITOR;
1717 ieee80211_process_delba(sdata, rx->sta, mgmt, len);
1718 break;
1719 }
Johannes Berg39192c02008-09-09 14:49:03 +02001720 break;
1721 case WLAN_CATEGORY_SPECTRUM_MGMT:
1722 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
1723 return RX_DROP_MONITOR;
Johannes Berg46900292009-02-15 12:44:28 +01001724
1725 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1726 return RX_DROP_MONITOR;
1727
Johannes Berg39192c02008-09-09 14:49:03 +02001728 switch (mgmt->u.action.u.measurement.action_code) {
1729 case WLAN_ACTION_SPCT_MSR_REQ:
1730 if (len < (IEEE80211_MIN_ACTION_SIZE +
1731 sizeof(mgmt->u.action.u.measurement)))
1732 return RX_DROP_MONITOR;
1733 ieee80211_process_measurement_req(sdata, mgmt, len);
1734 break;
Sujithc481ec92009-01-06 09:28:37 +05301735 case WLAN_ACTION_SPCT_CHL_SWITCH:
1736 if (len < (IEEE80211_MIN_ACTION_SIZE +
1737 sizeof(mgmt->u.action.u.chan_switch)))
1738 return RX_DROP_MONITOR;
1739
Johannes Bergcc32abd2009-05-15 11:52:31 +02001740 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1741 return RX_DROP_MONITOR;
1742
Johannes Berg46900292009-02-15 12:44:28 +01001743 if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
Sujithc481ec92009-01-06 09:28:37 +05301744 return RX_DROP_MONITOR;
1745
Johannes Berg77fdaa12009-07-07 03:45:17 +02001746 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
Johannes Berg39192c02008-09-09 14:49:03 +02001747 }
1748 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02001749 case WLAN_CATEGORY_SA_QUERY:
1750 if (len < (IEEE80211_MIN_ACTION_SIZE +
1751 sizeof(mgmt->u.action.u.sa_query)))
1752 return RX_DROP_MONITOR;
1753 switch (mgmt->u.action.u.sa_query.action) {
1754 case WLAN_ACTION_SA_QUERY_REQUEST:
1755 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1756 return RX_DROP_MONITOR;
1757 ieee80211_process_sa_query_req(sdata, mgmt, len);
1758 break;
1759 case WLAN_ACTION_SA_QUERY_RESPONSE:
1760 /*
1761 * SA Query response is currently only used in AP mode
1762 * and it is processed in user space.
1763 */
1764 return RX_CONTINUE;
1765 }
1766 break;
Johannes Berg39192c02008-09-09 14:49:03 +02001767 default:
1768 return RX_CONTINUE;
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001769 }
1770
Johannes Berg39192c02008-09-09 14:49:03 +02001771 rx->sta->rx_packets++;
1772 dev_kfree_skb(rx->skb);
1773 return RX_QUEUED;
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001774}
1775
1776static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001777ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001778{
Johannes Berg759ef3e2008-09-09 14:55:09 +02001779 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Jouni Malinen97ebe122009-01-08 13:32:08 +02001780 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001781
Johannes Berg5cf121c2008-02-25 16:27:43 +01001782 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001783 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001784
Jouni Malinen97ebe122009-01-08 13:32:08 +02001785 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1786 return RX_DROP_MONITOR;
1787
Johannes Berg472dbc42008-09-11 00:01:49 +02001788 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Bergf1d58c22009-06-17 13:13:00 +02001789 return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001790
Johannes Berg3832c282009-03-24 08:46:57 +01001791 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
Johannes Bergf1d58c22009-06-17 13:13:00 +02001792 return ieee80211_ibss_rx_mgmt(sdata, rx->skb);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001793
Johannes Berg7986cf92009-03-21 17:08:43 +01001794 if (sdata->vif.type == NL80211_IFTYPE_STATION)
Johannes Bergf1d58c22009-06-17 13:13:00 +02001795 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
Johannes Berg46900292009-02-15 12:44:28 +01001796
Johannes Berg7986cf92009-03-21 17:08:43 +01001797 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001798}
1799
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001800static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001801 struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001802{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001803 int keyidx;
1804 unsigned int hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001805
Harvey Harrisona7767f92008-07-02 16:30:51 -07001806 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001807 if (rx->skb->len >= hdrlen + 4)
1808 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1809 else
1810 keyidx = -1;
1811
Johannes Berg8944b792008-01-31 19:48:26 +01001812 if (!rx->sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001813 /*
1814 * Some hardware seem to generate incorrect Michael MIC
1815 * reports; ignore them to avoid triggering countermeasures.
1816 */
Johannes Bergaf2ced62009-11-16 12:00:39 +01001817 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001818 }
1819
Harvey Harrisona7767f92008-07-02 16:30:51 -07001820 if (!ieee80211_has_protected(hdr->frame_control))
Johannes Bergaf2ced62009-11-16 12:00:39 +01001821 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001822
Johannes Berg05c914f2008-09-11 00:01:58 +02001823 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001824 /*
1825 * APs with pairwise keys should never receive Michael MIC
1826 * errors for non-zero keyidx because these are reserved for
1827 * group keys and only the AP is sending real multicast
1828 * frames in the BSS.
1829 */
Johannes Bergaf2ced62009-11-16 12:00:39 +01001830 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001831 }
1832
Harvey Harrisona7767f92008-07-02 16:30:51 -07001833 if (!ieee80211_is_data(hdr->frame_control) &&
1834 !ieee80211_is_auth(hdr->frame_control))
Johannes Bergaf2ced62009-11-16 12:00:39 +01001835 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001836
Johannes Berge6d6e342009-07-01 21:26:47 +02001837 mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
1838 GFP_ATOMIC);
Johannes Berg571ecf62007-07-27 15:43:22 +02001839}
1840
Johannes Berg5cf121c2008-02-25 16:27:43 +01001841/* TODO: use IEEE80211_RX_FRAGMENTED */
1842static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx)
Michael Wu3d30d942008-01-31 19:48:27 +01001843{
1844 struct ieee80211_sub_if_data *sdata;
1845 struct ieee80211_local *local = rx->local;
1846 struct ieee80211_rtap_hdr {
1847 struct ieee80211_radiotap_header hdr;
1848 u8 flags;
1849 u8 rate;
1850 __le16 chan_freq;
1851 __le16 chan_flags;
1852 } __attribute__ ((packed)) *rthdr;
1853 struct sk_buff *skb = rx->skb, *skb2;
1854 struct net_device *prev_dev = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001855 struct ieee80211_rx_status *status = rx->status;
Michael Wu3d30d942008-01-31 19:48:27 +01001856
Johannes Berg5cf121c2008-02-25 16:27:43 +01001857 if (rx->flags & IEEE80211_RX_CMNTR_REPORTED)
Michael Wu3d30d942008-01-31 19:48:27 +01001858 goto out_free_skb;
1859
1860 if (skb_headroom(skb) < sizeof(*rthdr) &&
1861 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
1862 goto out_free_skb;
1863
1864 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
1865 memset(rthdr, 0, sizeof(*rthdr));
1866 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1867 rthdr->hdr.it_present =
1868 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1869 (1 << IEEE80211_RADIOTAP_RATE) |
1870 (1 << IEEE80211_RADIOTAP_CHANNEL));
1871
Johannes Berg5cf121c2008-02-25 16:27:43 +01001872 rthdr->rate = rx->rate->bitrate / 5;
Michael Wu3d30d942008-01-31 19:48:27 +01001873 rthdr->chan_freq = cpu_to_le16(status->freq);
1874
1875 if (status->band == IEEE80211_BAND_5GHZ)
1876 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
1877 IEEE80211_CHAN_5GHZ);
1878 else
1879 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
1880 IEEE80211_CHAN_2GHZ);
1881
1882 skb_set_mac_header(skb, 0);
1883 skb->ip_summed = CHECKSUM_UNNECESSARY;
1884 skb->pkt_type = PACKET_OTHERHOST;
1885 skb->protocol = htons(ETH_P_802_2);
1886
1887 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1888 if (!netif_running(sdata->dev))
1889 continue;
1890
Johannes Berg05c914f2008-09-11 00:01:58 +02001891 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Michael Wu3d30d942008-01-31 19:48:27 +01001892 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
1893 continue;
1894
1895 if (prev_dev) {
1896 skb2 = skb_clone(skb, GFP_ATOMIC);
1897 if (skb2) {
1898 skb2->dev = prev_dev;
1899 netif_rx(skb2);
1900 }
1901 }
1902
1903 prev_dev = sdata->dev;
1904 sdata->dev->stats.rx_packets++;
1905 sdata->dev->stats.rx_bytes += skb->len;
1906 }
1907
1908 if (prev_dev) {
1909 skb->dev = prev_dev;
1910 netif_rx(skb);
1911 skb = NULL;
1912 } else
1913 goto out_free_skb;
1914
Johannes Berg5cf121c2008-02-25 16:27:43 +01001915 rx->flags |= IEEE80211_RX_CMNTR_REPORTED;
Michael Wu3d30d942008-01-31 19:48:27 +01001916 return;
1917
1918 out_free_skb:
1919 dev_kfree_skb(skb);
1920}
1921
Johannes Berg571ecf62007-07-27 15:43:22 +02001922
Johannes Berg8944b792008-01-31 19:48:26 +01001923static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001924 struct ieee80211_rx_data *rx,
Johannes Berg8944b792008-01-31 19:48:26 +01001925 struct sk_buff *skb)
Johannes Berg58905292008-01-31 19:48:25 +01001926{
Johannes Berg58905292008-01-31 19:48:25 +01001927 ieee80211_rx_result res = RX_DROP_MONITOR;
1928
Johannes Berg8944b792008-01-31 19:48:26 +01001929 rx->skb = skb;
1930 rx->sdata = sdata;
1931 rx->dev = sdata->dev;
1932
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001933#define CALL_RXH(rxh) \
1934 do { \
1935 res = rxh(rx); \
1936 if (res != RX_CONTINUE) \
1937 goto rxh_done; \
1938 } while (0);
Johannes Berg58905292008-01-31 19:48:25 +01001939
Johannes Berg49461622008-06-30 15:10:45 +02001940 CALL_RXH(ieee80211_rx_h_passive_scan)
1941 CALL_RXH(ieee80211_rx_h_check)
1942 CALL_RXH(ieee80211_rx_h_decrypt)
Kalle Valo572e0012009-02-10 17:09:31 +02001943 CALL_RXH(ieee80211_rx_h_check_more_data)
Johannes Berg49461622008-06-30 15:10:45 +02001944 CALL_RXH(ieee80211_rx_h_sta_process)
1945 CALL_RXH(ieee80211_rx_h_defragment)
1946 CALL_RXH(ieee80211_rx_h_ps_poll)
1947 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
1948 /* must be after MMIC verify so header is counted in MPDU mic */
1949 CALL_RXH(ieee80211_rx_h_remove_qos_control)
1950 CALL_RXH(ieee80211_rx_h_amsdu)
Ingo Molnarbf94e172008-10-12 23:51:38 -07001951#ifdef CONFIG_MAC80211_MESH
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001952 if (ieee80211_vif_is_mesh(&sdata->vif))
1953 CALL_RXH(ieee80211_rx_h_mesh_fwding);
Ingo Molnarbf94e172008-10-12 23:51:38 -07001954#endif
Johannes Berg49461622008-06-30 15:10:45 +02001955 CALL_RXH(ieee80211_rx_h_data)
1956 CALL_RXH(ieee80211_rx_h_ctrl)
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001957 CALL_RXH(ieee80211_rx_h_action)
Johannes Berg49461622008-06-30 15:10:45 +02001958 CALL_RXH(ieee80211_rx_h_mgmt)
Johannes Berg58905292008-01-31 19:48:25 +01001959
Johannes Berg49461622008-06-30 15:10:45 +02001960#undef CALL_RXH
1961
1962 rxh_done:
Johannes Berg58905292008-01-31 19:48:25 +01001963 switch (res) {
Michael Wu3d30d942008-01-31 19:48:27 +01001964 case RX_DROP_MONITOR:
Johannes Berg49461622008-06-30 15:10:45 +02001965 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1966 if (rx->sta)
1967 rx->sta->rx_dropped++;
1968 /* fall through */
1969 case RX_CONTINUE:
Michael Wu3d30d942008-01-31 19:48:27 +01001970 ieee80211_rx_cooked_monitor(rx);
1971 break;
1972 case RX_DROP_UNUSABLE:
Johannes Berg49461622008-06-30 15:10:45 +02001973 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1974 if (rx->sta)
1975 rx->sta->rx_dropped++;
Johannes Berg58905292008-01-31 19:48:25 +01001976 dev_kfree_skb(rx->skb);
1977 break;
Johannes Berg49461622008-06-30 15:10:45 +02001978 case RX_QUEUED:
1979 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
1980 break;
Johannes Berg58905292008-01-31 19:48:25 +01001981 }
1982}
1983
Johannes Berg571ecf62007-07-27 15:43:22 +02001984/* main receive path */
1985
Johannes Berg23a24de2007-07-27 15:43:22 +02001986static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg7ab17c42009-02-10 21:25:43 +01001987 struct ieee80211_rx_data *rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001988 struct ieee80211_hdr *hdr)
1989{
Johannes Berg7ab17c42009-02-10 21:25:43 +01001990 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len, sdata->vif.type);
Johannes Berg23a24de2007-07-27 15:43:22 +02001991 int multicast = is_multicast_ether_addr(hdr->addr1);
1992
Johannes Berg51fb61e2007-12-19 01:31:27 +01001993 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001994 case NL80211_IFTYPE_STATION:
Felix Fietkauf14543e2009-11-10 20:10:05 +01001995 if (!bssid && !sdata->use_4addr)
Johannes Berg23a24de2007-07-27 15:43:22 +02001996 return 0;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001997 if (!multicast &&
1998 compare_ether_addr(sdata->dev->dev_addr, hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001999 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02002000 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002001 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002002 }
2003 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002004 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02002005 if (!bssid)
2006 return 0;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002007 if (ieee80211_is_beacon(hdr->frame_control)) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002008 return 1;
Vladimir Koutny87291c02008-06-13 16:50:44 +02002009 }
Johannes Berg46900292009-02-15 12:44:28 +01002010 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01002011 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02002012 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002013 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002014 } else if (!multicast &&
2015 compare_ether_addr(sdata->dev->dev_addr,
2016 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04002017 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02002018 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002019 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002020 } else if (!rx->sta) {
2021 int rate_idx;
2022 if (rx->status->flag & RX_FLAG_HT)
2023 rate_idx = 0; /* TODO: HT rates */
2024 else
2025 rate_idx = rx->status->rate_idx;
Rami Rosenab1f5c02008-12-11 14:00:25 +02002026 rx->sta = ieee80211_ibss_add_sta(sdata, bssid, hdr->addr2,
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002027 BIT(rate_idx));
2028 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002029 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002030 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg6032f932008-02-23 15:17:07 +01002031 if (!multicast &&
2032 compare_ether_addr(sdata->dev->dev_addr,
2033 hdr->addr1) != 0) {
2034 if (!(sdata->dev->flags & IFF_PROMISC))
2035 return 0;
2036
Johannes Berg5cf121c2008-02-25 16:27:43 +01002037 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg6032f932008-02-23 15:17:07 +01002038 }
2039 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002040 case NL80211_IFTYPE_AP_VLAN:
2041 case NL80211_IFTYPE_AP:
Johannes Berg23a24de2007-07-27 15:43:22 +02002042 if (!bssid) {
2043 if (compare_ether_addr(sdata->dev->dev_addr,
2044 hdr->addr1))
2045 return 0;
2046 } else if (!ieee80211_bssid_match(bssid,
2047 sdata->dev->dev_addr)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01002048 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02002049 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002050 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002051 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002052 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002053 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07002054 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg23a24de2007-07-27 15:43:22 +02002055 return 0;
2056 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2057 return 0;
2058 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002059 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +02002060 case NL80211_IFTYPE_UNSPECIFIED:
2061 case __NL80211_IFTYPE_AFTER_LAST:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02002062 /* should never get here */
2063 WARN_ON(1);
2064 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02002065 }
2066
2067 return 1;
2068}
2069
Johannes Berg571ecf62007-07-27 15:43:22 +02002070/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002071 * This is the actual Rx frames handler. as it blongs to Rx path it must
2072 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02002073 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02002074static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2075 struct sk_buff *skb,
Johannes Berg8318d782008-01-24 19:38:38 +01002076 struct ieee80211_rate *rate)
Johannes Berg571ecf62007-07-27 15:43:22 +02002077{
Johannes Bergf1d58c22009-06-17 13:13:00 +02002078 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002079 struct ieee80211_local *local = hw_to_local(hw);
2080 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002081 struct ieee80211_hdr *hdr;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002082 struct ieee80211_rx_data rx;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002083 int prepares;
Johannes Berg8e6f0032007-07-27 15:43:22 +02002084 struct ieee80211_sub_if_data *prev = NULL;
2085 struct sk_buff *skb_new;
Johannes Berg571ecf62007-07-27 15:43:22 +02002086
Harvey Harrison358c8d92008-07-15 18:44:13 -07002087 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002088 memset(&rx, 0, sizeof(rx));
2089 rx.skb = skb;
2090 rx.local = local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002091 rx.status = status;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002092 rx.rate = rate;
Johannes Berg72abd812007-09-17 01:29:22 -04002093
Harvey Harrison358c8d92008-07-15 18:44:13 -07002094 if (ieee80211_is_data(hdr->frame_control) || ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg571ecf62007-07-27 15:43:22 +02002095 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02002096
Helmut Schaa142b9f52009-07-23 13:18:01 +02002097 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2098 test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
Johannes Berg5cf121c2008-02-25 16:27:43 +01002099 rx.flags |= IEEE80211_RX_IN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02002100
Johannes Berg38f37142008-01-29 17:07:43 +01002101 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002102 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01002103
Johannes Bergaf2ced62009-11-16 12:00:39 +01002104 rx.sta = sta_info_get(local, hdr->addr2);
2105 if (rx.sta) {
2106 rx.sdata = rx.sta->sdata;
2107 rx.dev = rx.sta->sdata->dev;
2108 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002109
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002110 if (rx.sdata && ieee80211_is_data(hdr->frame_control)) {
2111 rx.flags |= IEEE80211_RX_RA_MATCH;
2112 prepares = prepare_for_handlers(rx.sdata, &rx, hdr);
Johannes Bergaf2ced62009-11-16 12:00:39 +01002113 if (prepares) {
2114 if (status->flag & RX_FLAG_MMIC_ERROR) {
2115 if (rx.flags & IEEE80211_RX_RA_MATCH)
2116 ieee80211_rx_michael_mic_report(hdr, &rx);
2117 } else
2118 prev = rx.sdata;
2119 }
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002120 } else list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04002121 if (!netif_running(sdata->dev))
2122 continue;
2123
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002124 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2125 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Bergb2e77712007-09-26 15:19:39 +02002126 continue;
2127
Johannes Berg5cf121c2008-02-25 16:27:43 +01002128 rx.flags |= IEEE80211_RX_RA_MATCH;
Johannes Berg7ab17c42009-02-10 21:25:43 +01002129 prepares = prepare_for_handlers(sdata, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02002130
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002131 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02002132 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02002133
Johannes Bergaf2ced62009-11-16 12:00:39 +01002134 if (status->flag & RX_FLAG_MMIC_ERROR) {
2135 rx.sdata = sdata;
2136 if (rx.flags & IEEE80211_RX_RA_MATCH)
2137 ieee80211_rx_michael_mic_report(hdr, &rx);
2138 continue;
2139 }
2140
Johannes Berg340e11f2007-07-27 15:43:22 +02002141 /*
2142 * frame is destined for this interface, but if it's not
2143 * also for the previous one we handle that after the
2144 * loop to avoid copying the SKB once too much
2145 */
2146
2147 if (!prev) {
2148 prev = sdata;
2149 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02002150 }
Johannes Berg340e11f2007-07-27 15:43:22 +02002151
2152 /*
2153 * frame was destined for the previous interface
2154 * so invoke RX handlers for it
2155 */
2156
2157 skb_new = skb_copy(skb, GFP_ATOMIC);
2158 if (!skb_new) {
2159 if (net_ratelimit())
2160 printk(KERN_DEBUG "%s: failed to copy "
Pavel Roskina4278e12008-05-12 09:02:24 -04002161 "multicast frame for %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04002162 wiphy_name(local->hw.wiphy),
2163 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02002164 continue;
2165 }
Johannes Berg8944b792008-01-31 19:48:26 +01002166 ieee80211_invoke_rx_handlers(prev, &rx, skb_new);
Johannes Berg8e6f0032007-07-27 15:43:22 +02002167 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002168 }
Harvey Harrisona4b7d7b2008-07-15 18:44:14 -07002169 if (prev)
Johannes Berg8944b792008-01-31 19:48:26 +01002170 ieee80211_invoke_rx_handlers(prev, &rx, skb);
Harvey Harrisona4b7d7b2008-07-15 18:44:14 -07002171 else
Johannes Berg8e6f0032007-07-27 15:43:22 +02002172 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002173}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002174
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002175#define SEQ_MODULO 0x1000
2176#define SEQ_MASK 0xfff
2177
2178static inline int seq_less(u16 sq1, u16 sq2)
2179{
Johannes Bergc6a1fa12008-10-07 12:04:32 +02002180 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002181}
2182
2183static inline u16 seq_inc(u16 sq)
2184{
Johannes Bergc6a1fa12008-10-07 12:04:32 +02002185 return (sq + 1) & SEQ_MASK;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002186}
2187
2188static inline u16 seq_sub(u16 sq1, u16 sq2)
2189{
Johannes Bergc6a1fa12008-10-07 12:04:32 +02002190 return (sq1 - sq2) & SEQ_MASK;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002191}
2192
2193
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002194static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
2195 struct tid_ampdu_rx *tid_agg_rx,
2196 int index)
2197{
2198 struct ieee80211_supported_band *sband;
2199 struct ieee80211_rate *rate;
Johannes Bergf1d58c22009-06-17 13:13:00 +02002200 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
2201 struct ieee80211_rx_status *status;
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002202
Johannes Bergf1d58c22009-06-17 13:13:00 +02002203 if (!skb)
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002204 goto no_frame;
2205
Johannes Bergf1d58c22009-06-17 13:13:00 +02002206 status = IEEE80211_SKB_RXCB(skb);
2207
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002208 /* release the reordered frames to stack */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002209 sband = hw->wiphy->bands[status->band];
2210 if (status->flag & RX_FLAG_HT)
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002211 rate = sband->bitrates; /* TODO: HT rates */
2212 else
Johannes Bergf1d58c22009-06-17 13:13:00 +02002213 rate = &sband->bitrates[status->rate_idx];
2214 __ieee80211_rx_handle_packet(hw, skb, rate);
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002215 tid_agg_rx->stored_mpdu_num--;
2216 tid_agg_rx->reorder_buf[index] = NULL;
2217
2218no_frame:
2219 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2220}
2221
Johannes Berga02ae752009-11-16 12:00:40 +01002222static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
2223 struct tid_ampdu_rx *tid_agg_rx,
2224 u16 head_seq_num)
2225{
2226 int index;
2227
2228 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2229 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
2230 tid_agg_rx->buf_size;
2231 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
2232 }
2233}
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002234
Ron Rindjunsky71364712007-12-25 17:00:36 +02002235/*
Jouni Malinen4d050f12009-05-05 20:35:14 +03002236 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
2237 * the skb was added to the buffer longer than this time ago, the earlier
2238 * frames that have not yet been received are assumed to be lost and the skb
2239 * can be released for processing. This may also release other skb's from the
2240 * reorder buffer if there are no additional gaps between the frames.
2241 */
2242#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
2243
2244/*
Johannes Berga02ae752009-11-16 12:00:40 +01002245 * As this function belongs to the RX path it must be under
2246 * rcu_read_lock protection. It returns false if the frame
2247 * can be processed immediately, true if it was consumed.
Ron Rindjunsky71364712007-12-25 17:00:36 +02002248 */
Johannes Berga02ae752009-11-16 12:00:40 +01002249static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
2250 struct tid_ampdu_rx *tid_agg_rx,
2251 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002252{
Johannes Berga02ae752009-11-16 12:00:40 +01002253 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2254 u16 sc = le16_to_cpu(hdr->seq_ctrl);
2255 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002256 u16 head_seq_num, buf_size;
2257 int index;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002258
2259 buf_size = tid_agg_rx->buf_size;
2260 head_seq_num = tid_agg_rx->head_seq_num;
2261
2262 /* frame with out of date sequence number */
2263 if (seq_less(mpdu_seq_num, head_seq_num)) {
2264 dev_kfree_skb(skb);
Johannes Berga02ae752009-11-16 12:00:40 +01002265 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002266 }
2267
Johannes Berga02ae752009-11-16 12:00:40 +01002268 /*
2269 * If frame the sequence number exceeds our buffering window
2270 * size release some previous frames to make room for this one.
2271 */
2272 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
2273 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002274 /* release stored frames up to new head to stack */
Johannes Berga02ae752009-11-16 12:00:40 +01002275 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002276 }
2277
Johannes Berga02ae752009-11-16 12:00:40 +01002278 /* Now the new frame is always in the range of the reordering buffer */
2279
2280 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
2281
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002282 /* check if we already stored this frame */
2283 if (tid_agg_rx->reorder_buf[index]) {
2284 dev_kfree_skb(skb);
Johannes Berga02ae752009-11-16 12:00:40 +01002285 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002286 }
2287
Johannes Berga02ae752009-11-16 12:00:40 +01002288 /*
2289 * If the current MPDU is in the right order and nothing else
2290 * is stored we can process it directly, no need to buffer it.
2291 */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002292 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
Johannes Berga02ae752009-11-16 12:00:40 +01002293 tid_agg_rx->stored_mpdu_num == 0) {
2294 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2295 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002296 }
2297
2298 /* put the frame in the reordering buffer */
2299 tid_agg_rx->reorder_buf[index] = skb;
Jouni Malinen4d050f12009-05-05 20:35:14 +03002300 tid_agg_rx->reorder_time[index] = jiffies;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002301 tid_agg_rx->stored_mpdu_num++;
2302 /* release the buffer until next missing frame */
Johannes Berga02ae752009-11-16 12:00:40 +01002303 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
2304 tid_agg_rx->buf_size;
Jouni Malinen4d050f12009-05-05 20:35:14 +03002305 if (!tid_agg_rx->reorder_buf[index] &&
2306 tid_agg_rx->stored_mpdu_num > 1) {
2307 /*
2308 * No buffers ready to be released, but check whether any
2309 * frames in the reorder buffer have timed out.
2310 */
2311 int j;
2312 int skipped = 1;
2313 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
2314 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Berga02ae752009-11-16 12:00:40 +01002315 if (!tid_agg_rx->reorder_buf[j]) {
Jouni Malinen4d050f12009-05-05 20:35:14 +03002316 skipped++;
2317 continue;
2318 }
2319 if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
Johannes Berga02ae752009-11-16 12:00:40 +01002320 HT_RX_REORDER_BUF_TIMEOUT))
Jouni Malinen4d050f12009-05-05 20:35:14 +03002321 break;
2322
2323#ifdef CONFIG_MAC80211_HT_DEBUG
2324 if (net_ratelimit())
2325 printk(KERN_DEBUG "%s: release an RX reorder "
2326 "frame due to timeout on earlier "
2327 "frames\n",
2328 wiphy_name(hw->wiphy));
2329#endif
2330 ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
2331
2332 /*
2333 * Increment the head seq# also for the skipped slots.
2334 */
2335 tid_agg_rx->head_seq_num =
Johannes Berga02ae752009-11-16 12:00:40 +01002336 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
Jouni Malinen4d050f12009-05-05 20:35:14 +03002337 skipped = 0;
2338 }
2339 } else while (tid_agg_rx->reorder_buf[index]) {
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002340 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
Johannes Berga02ae752009-11-16 12:00:40 +01002341 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
2342 tid_agg_rx->buf_size;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002343 }
Johannes Berga02ae752009-11-16 12:00:40 +01002344
2345 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002346}
2347
Johannes Berga02ae752009-11-16 12:00:40 +01002348/*
2349 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
2350 * true if the MPDU was buffered, false if it should be processed.
2351 */
2352static bool ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
2353 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002354{
2355 struct ieee80211_hw *hw = &local->hw;
2356 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2357 struct sta_info *sta;
2358 struct tid_ampdu_rx *tid_agg_rx;
Harvey Harrison87228f52008-06-11 14:21:59 -07002359 u16 sc;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002360 int tid;
2361
Johannes Berga02ae752009-11-16 12:00:40 +01002362 if (!ieee80211_is_data_qos(hdr->frame_control))
2363 return false;
2364
2365 /*
2366 * filter the QoS data rx stream according to
2367 * STA/TID and check if this STA/TID is on aggregation
2368 */
2369
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002370 sta = sta_info_get(local, hdr->addr2);
2371 if (!sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002372 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002373
Harvey Harrison238f74a2008-07-02 11:05:34 -07002374 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002375
Ron Rindjunskycee24a32008-03-26 20:36:03 +02002376 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL)
Johannes Berga02ae752009-11-16 12:00:40 +01002377 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002378
Ron Rindjunskycee24a32008-03-26 20:36:03 +02002379 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
2380
Emmanuel Grumbach2560b6e2008-07-10 00:47:19 +03002381 /* qos null data frames are excluded */
2382 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga02ae752009-11-16 12:00:40 +01002383 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002384
Johannes Berga02ae752009-11-16 12:00:40 +01002385 /* new, potentially un-ordered, ampdu frame - process it */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002386
2387 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01002388 if (tid_agg_rx->timeout)
2389 mod_timer(&tid_agg_rx->session_timer,
2390 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002391
2392 /* if this mpdu is fragmented - terminate rx aggregation session */
2393 sc = le16_to_cpu(hdr->seq_ctrl);
2394 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Berg17741cd2008-09-11 00:02:02 +02002395 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002396 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga02ae752009-11-16 12:00:40 +01002397 dev_kfree_skb(skb);
2398 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002399 }
2400
Johannes Berga02ae752009-11-16 12:00:40 +01002401 return ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002402}
2403
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002404/*
2405 * This is the receive path handler. It is called by a low level driver when an
2406 * 802.11 MPDU is received from the hardware.
2407 */
John W. Linville103bf9f2009-08-20 16:34:15 -04002408void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002409{
2410 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01002411 struct ieee80211_rate *rate = NULL;
2412 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02002413 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01002414
Johannes Bergd20ef632009-10-11 15:10:40 +02002415 WARN_ON_ONCE(softirq_count() == 0);
2416
Johannes Berg77a980d2009-08-24 11:46:30 +02002417 if (WARN_ON(status->band < 0 ||
2418 status->band >= IEEE80211_NUM_BANDS))
2419 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01002420
2421 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02002422 if (WARN_ON(!sband))
2423 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01002424
Johannes Berg89c3a8a2009-07-28 18:10:17 +02002425 /*
2426 * If we're suspending, it is possible although not too likely
2427 * that we'd be receiving frames after having already partially
2428 * quiesced the stack. We can't process such frames then since
2429 * that might, for example, cause stations to be added or other
2430 * driver callbacks be invoked.
2431 */
Johannes Berg77a980d2009-08-24 11:46:30 +02002432 if (unlikely(local->quiescing || local->suspended))
2433 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02002434
Johannes Bergea77f122009-08-21 14:44:45 +02002435 /*
2436 * The same happens when we're not even started,
2437 * but that's worth a warning.
2438 */
Johannes Berg77a980d2009-08-24 11:46:30 +02002439 if (WARN_ON(!local->started))
2440 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02002441
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002442 if (status->flag & RX_FLAG_HT) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05002443 /*
2444 * rate_idx is MCS index, which can be [0-76] as documented on:
2445 *
2446 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
2447 *
2448 * Anything else would be some sort of driver or hardware error.
2449 * The driver should catch hardware errors.
2450 */
2451 if (WARN((status->rate_idx < 0 ||
2452 status->rate_idx > 76),
2453 "Rate marked as an HT rate but passed "
2454 "status->rate_idx is not "
2455 "an MCS index [0-76]: %d (0x%02x)\n",
2456 status->rate_idx,
2457 status->rate_idx))
Johannes Berg77a980d2009-08-24 11:46:30 +02002458 goto drop;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002459 /* HT rates are not in the table - use the highest legacy rate
2460 * for now since other parts of mac80211 may not yet be fully
2461 * MCS aware. */
2462 rate = &sband->bitrates[sband->n_bitrates - 1];
2463 } else {
2464 if (WARN_ON(status->rate_idx < 0 ||
2465 status->rate_idx >= sband->n_bitrates))
Johannes Berg77a980d2009-08-24 11:46:30 +02002466 goto drop;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002467 rate = &sband->bitrates[status->rate_idx];
2468 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002469
2470 /*
2471 * key references and virtual interfaces are protected using RCU
2472 * and this requires that we are in a read-side RCU section during
2473 * receive processing
2474 */
2475 rcu_read_lock();
2476
2477 /*
2478 * Frames with failed FCS/PLCP checksum are not returned,
2479 * all other frames are returned without radiotap header
2480 * if it was previously present.
2481 * Also, frames with less than 16 bytes are dropped.
2482 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002483 skb = ieee80211_rx_monitor(local, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002484 if (!skb) {
2485 rcu_read_unlock();
2486 return;
2487 }
2488
Jouni Malinenaec67952009-05-05 20:35:15 +03002489 /*
2490 * In theory, the block ack reordering should happen after duplicate
2491 * removal (ieee80211_rx_h_check(), which is an RX handler). As such,
2492 * the call to ieee80211_rx_reorder_ampdu() should really be moved to
2493 * happen as a new RX handler between ieee80211_rx_h_check and
2494 * ieee80211_rx_h_decrypt. This cleanup may eventually happen, but for
2495 * the time being, the call can be here since RX reorder buf processing
2496 * will implicitly skip duplicates. We could, in theory at least,
2497 * process frames that ieee80211_rx_h_passive_scan would drop (e.g.,
2498 * frames from other than operational channel), but that should not
2499 * happen in normal networks.
2500 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002501 if (!ieee80211_rx_reorder_ampdu(local, skb))
2502 __ieee80211_rx_handle_packet(hw, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002503
2504 rcu_read_unlock();
Johannes Berg77a980d2009-08-24 11:46:30 +02002505
2506 return;
2507 drop:
2508 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002509}
John W. Linville103bf9f2009-08-20 16:34:15 -04002510EXPORT_SYMBOL(ieee80211_rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002511
2512/* This is a version of the rx handler that can be called from hard irq
2513 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002514void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02002515{
2516 struct ieee80211_local *local = hw_to_local(hw);
2517
2518 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2519
Johannes Berg571ecf62007-07-27 15:43:22 +02002520 skb->pkt_type = IEEE80211_RX_MSG;
2521 skb_queue_tail(&local->skb_queue, skb);
2522 tasklet_schedule(&local->tasklet);
2523}
2524EXPORT_SYMBOL(ieee80211_rx_irqsafe);