blob: 6b50b6c12da38ce11ab1f300154f843e11bd76a3 [file] [log] [blame]
Johannes Bergc2d15602007-07-27 15:43:23 +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 * utilities for mac80211
12 */
13
14#include <net/mac80211.h>
15#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/etherdevice.h>
20#include <linux/if_arp.h>
21#include <linux/wireless.h>
22#include <linux/bitmap.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070023#include <net/net_namespace.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020024#include <net/cfg80211.h>
Johannes Bergdabeb342007-11-09 01:57:29 +010025#include <net/rtnetlink.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020026
27#include "ieee80211_i.h"
28#include "ieee80211_rate.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010029#ifdef CONFIG_MAC80211_MESH
30#include "mesh.h"
31#endif
Johannes Bergc2d15602007-07-27 15:43:23 +020032#include "wme.h"
33
34/* privid for wiphys to determine whether they belong to us or not */
35void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
36
37/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
38/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
39const unsigned char rfc1042_header[] =
40 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
41
42/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
43const unsigned char bridge_tunnel_header[] =
44 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
45
Johannes Bergc2d15602007-07-27 15:43:23 +020046
Ron Rindjunsky71364712007-12-25 17:00:36 +020047u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
48 enum ieee80211_if_types type)
Johannes Bergc2d15602007-07-27 15:43:23 +020049{
50 u16 fc;
51
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020052 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
53 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020054 return NULL;
55
56 fc = le16_to_cpu(hdr->frame_control);
57
58 switch (fc & IEEE80211_FCTL_FTYPE) {
59 case IEEE80211_FTYPE_DATA:
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020060 if (len < 24) /* drop incorrect hdr len (data) */
61 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020062 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
63 case IEEE80211_FCTL_TODS:
64 return hdr->addr1;
65 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
66 return NULL;
67 case IEEE80211_FCTL_FROMDS:
68 return hdr->addr2;
69 case 0:
70 return hdr->addr3;
71 }
72 break;
73 case IEEE80211_FTYPE_MGMT:
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020074 if (len < 24) /* drop incorrect hdr len (mgmt) */
75 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020076 return hdr->addr3;
77 case IEEE80211_FTYPE_CTL:
78 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
79 return hdr->addr1;
Ron Rindjunsky71364712007-12-25 17:00:36 +020080 else if ((fc & IEEE80211_FCTL_STYPE) ==
81 IEEE80211_STYPE_BACK_REQ) {
82 switch (type) {
83 case IEEE80211_IF_TYPE_STA:
84 return hdr->addr2;
85 case IEEE80211_IF_TYPE_AP:
86 case IEEE80211_IF_TYPE_VLAN:
87 return hdr->addr1;
88 default:
89 return NULL;
90 }
91 }
Johannes Bergc2d15602007-07-27 15:43:23 +020092 else
93 return NULL;
94 }
95
96 return NULL;
97}
98
99int ieee80211_get_hdrlen(u16 fc)
100{
101 int hdrlen = 24;
102
103 switch (fc & IEEE80211_FCTL_FTYPE) {
104 case IEEE80211_FTYPE_DATA:
105 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
106 hdrlen = 30; /* Addr4 */
107 /*
108 * The QoS Control field is two bytes and its presence is
109 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
110 * hdrlen if that bit is set.
111 * This works by masking out the bit and shifting it to
112 * bit position 1 so the result has the value 0 or 2.
113 */
114 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
115 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
116 break;
117 case IEEE80211_FTYPE_CTL:
118 /*
119 * ACK and CTS are 10 bytes, all others 16. To see how
120 * to get this condition consider
121 * subtype mask: 0b0000000011110000 (0x00F0)
122 * ACK subtype: 0b0000000011010000 (0x00D0)
123 * CTS subtype: 0b0000000011000000 (0x00C0)
124 * bits that matter: ^^^ (0x00E0)
125 * value of those: 0b0000000011000000 (0x00C0)
126 */
127 if ((fc & 0xE0) == 0xC0)
128 hdrlen = 10;
129 else
130 hdrlen = 16;
131 break;
132 }
133
134 return hdrlen;
135}
136EXPORT_SYMBOL(ieee80211_get_hdrlen);
137
138int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
139{
140 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
141 int hdrlen;
142
143 if (unlikely(skb->len < 10))
144 return 0;
145 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
146 if (unlikely(hdrlen > skb->len))
147 return 0;
148 return hdrlen;
149}
150EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
151
Luis Carlos Coboee385852008-02-23 15:17:11 +0100152#ifdef CONFIG_MAC80211_MESH
153int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
154{
155 int ae = meshhdr->flags & IEEE80211S_FLAGS_AE;
156 /* 7.1.3.5a.2 */
157 switch (ae) {
158 case 0:
159 return 5;
160 case 1:
161 return 11;
162 case 2:
163 return 17;
164 case 3:
165 return 23;
166 default:
167 return 5;
168 }
169}
170#endif
171
Johannes Bergc2d15602007-07-27 15:43:23 +0200172void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
173{
174 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
175
176 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
177 if (tx->u.tx.extra_frag) {
178 struct ieee80211_hdr *fhdr;
179 int i;
180 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
181 fhdr = (struct ieee80211_hdr *)
182 tx->u.tx.extra_frag[i]->data;
183 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
184 }
185 }
186}
187
188int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
189 int rate, int erp, int short_preamble)
190{
191 int dur;
192
193 /* calculate duration (in microseconds, rounded up to next higher
194 * integer if it includes a fractional microsecond) to send frame of
195 * len bytes (does not include FCS) at the given rate. Duration will
196 * also include SIFS.
197 *
198 * rate is in 100 kbps, so divident is multiplied by 10 in the
199 * DIV_ROUND_UP() operations.
200 */
201
Johannes Berg8318d782008-01-24 19:38:38 +0100202 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200203 /*
204 * OFDM:
205 *
206 * N_DBPS = DATARATE x 4
207 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
208 * (16 = SIGNAL time, 6 = tail bits)
209 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
210 *
211 * T_SYM = 4 usec
212 * 802.11a - 17.5.2: aSIFSTime = 16 usec
213 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
214 * signal ext = 6 usec
215 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200216 dur = 16; /* SIFS + signal ext */
217 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
218 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
219 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
220 4 * rate); /* T_SYM x N_SYM */
221 } else {
222 /*
223 * 802.11b or 802.11g with 802.11b compatibility:
224 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
225 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
226 *
227 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
228 * aSIFSTime = 10 usec
229 * aPreambleLength = 144 usec or 72 usec with short preamble
230 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
231 */
232 dur = 10; /* aSIFSTime = 10 usec */
233 dur += short_preamble ? (72 + 24) : (144 + 48);
234
235 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
236 }
237
238 return dur;
239}
240
241/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100242__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
243 struct ieee80211_vif *vif,
Johannes Berg8318d782008-01-24 19:38:38 +0100244 size_t frame_len,
245 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200246{
247 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg32bfd352007-12-19 01:31:26 +0100248 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Johannes Bergc2d15602007-07-27 15:43:23 +0200249 u16 dur;
250 int erp;
251
Johannes Berg8318d782008-01-24 19:38:38 +0100252 erp = 0;
253 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
254 erp = rate->flags & IEEE80211_RATE_ERP_G;
255
256 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100257 sdata->bss_conf.use_short_preamble);
Johannes Bergc2d15602007-07-27 15:43:23 +0200258
259 return cpu_to_le16(dur);
260}
261EXPORT_SYMBOL(ieee80211_generic_frame_duration);
262
Johannes Berg32bfd352007-12-19 01:31:26 +0100263__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
264 struct ieee80211_vif *vif, size_t frame_len,
Johannes Bergc2d15602007-07-27 15:43:23 +0200265 const struct ieee80211_tx_control *frame_txctl)
266{
267 struct ieee80211_local *local = hw_to_local(hw);
268 struct ieee80211_rate *rate;
Johannes Berg32bfd352007-12-19 01:31:26 +0100269 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100270 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200271 int erp;
272 u16 dur;
273
Johannes Berg471b3ef2007-12-28 14:32:58 +0100274 short_preamble = sdata->bss_conf.use_short_preamble;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200275
Johannes Berg8318d782008-01-24 19:38:38 +0100276 rate = frame_txctl->rts_cts_rate;
277
278 erp = 0;
279 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
280 erp = rate->flags & IEEE80211_RATE_ERP_G;
Johannes Bergc2d15602007-07-27 15:43:23 +0200281
282 /* CTS duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100283 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200284 erp, short_preamble);
285 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100286 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200287 erp, short_preamble);
288 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100289 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200290 erp, short_preamble);
291
292 return cpu_to_le16(dur);
293}
294EXPORT_SYMBOL(ieee80211_rts_duration);
295
Johannes Berg32bfd352007-12-19 01:31:26 +0100296__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
297 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200298 size_t frame_len,
299 const struct ieee80211_tx_control *frame_txctl)
300{
301 struct ieee80211_local *local = hw_to_local(hw);
302 struct ieee80211_rate *rate;
Johannes Berg32bfd352007-12-19 01:31:26 +0100303 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100304 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200305 int erp;
306 u16 dur;
307
Johannes Berg471b3ef2007-12-28 14:32:58 +0100308 short_preamble = sdata->bss_conf.use_short_preamble;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200309
Johannes Berg8318d782008-01-24 19:38:38 +0100310 rate = frame_txctl->rts_cts_rate;
311 erp = 0;
312 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
313 erp = rate->flags & IEEE80211_RATE_ERP_G;
Johannes Bergc2d15602007-07-27 15:43:23 +0200314
315 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100316 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200317 erp, short_preamble);
318 if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
319 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100320 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200321 erp, short_preamble);
322 }
323
Johannes Bergc2d15602007-07-27 15:43:23 +0200324 return cpu_to_le16(dur);
325}
326EXPORT_SYMBOL(ieee80211_ctstoself_duration);
327
Johannes Bergc2d15602007-07-27 15:43:23 +0200328void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
329{
330 struct ieee80211_local *local = hw_to_local(hw);
331
332 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
333 &local->state[queue])) {
334 if (test_bit(IEEE80211_LINK_STATE_PENDING,
335 &local->state[queue]))
336 tasklet_schedule(&local->tx_pending_tasklet);
337 else
338 if (!ieee80211_qdisc_installed(local->mdev)) {
339 if (queue == 0)
340 netif_wake_queue(local->mdev);
341 } else
342 __netif_schedule(local->mdev);
343 }
344}
345EXPORT_SYMBOL(ieee80211_wake_queue);
346
347void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
348{
349 struct ieee80211_local *local = hw_to_local(hw);
350
351 if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
352 netif_stop_queue(local->mdev);
353 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
354}
355EXPORT_SYMBOL(ieee80211_stop_queue);
356
357void ieee80211_start_queues(struct ieee80211_hw *hw)
358{
359 struct ieee80211_local *local = hw_to_local(hw);
360 int i;
361
362 for (i = 0; i < local->hw.queues; i++)
363 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
364 if (!ieee80211_qdisc_installed(local->mdev))
365 netif_start_queue(local->mdev);
366}
367EXPORT_SYMBOL(ieee80211_start_queues);
368
369void ieee80211_stop_queues(struct ieee80211_hw *hw)
370{
371 int i;
372
373 for (i = 0; i < hw->queues; i++)
374 ieee80211_stop_queue(hw, i);
375}
376EXPORT_SYMBOL(ieee80211_stop_queues);
377
378void ieee80211_wake_queues(struct ieee80211_hw *hw)
379{
380 int i;
381
382 for (i = 0; i < hw->queues; i++)
383 ieee80211_wake_queue(hw, i);
384}
385EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100386
Johannes Berg32bfd352007-12-19 01:31:26 +0100387void ieee80211_iterate_active_interfaces(
388 struct ieee80211_hw *hw,
389 void (*iterator)(void *data, u8 *mac,
390 struct ieee80211_vif *vif),
391 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100392{
393 struct ieee80211_local *local = hw_to_local(hw);
394 struct ieee80211_sub_if_data *sdata;
395
Johannes Berge38bad42007-11-28 10:55:32 +0100396 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100397
Johannes Berge38bad42007-11-28 10:55:32 +0100398 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100399 switch (sdata->vif.type) {
Johannes Bergdabeb342007-11-09 01:57:29 +0100400 case IEEE80211_IF_TYPE_INVALID:
401 case IEEE80211_IF_TYPE_MNTR:
402 case IEEE80211_IF_TYPE_VLAN:
403 continue;
404 case IEEE80211_IF_TYPE_AP:
405 case IEEE80211_IF_TYPE_STA:
406 case IEEE80211_IF_TYPE_IBSS:
407 case IEEE80211_IF_TYPE_WDS:
Johannes Berg6032f932008-02-23 15:17:07 +0100408 case IEEE80211_IF_TYPE_MESH_POINT:
Johannes Bergdabeb342007-11-09 01:57:29 +0100409 break;
410 }
411 if (sdata->dev == local->mdev)
412 continue;
413 if (netif_running(sdata->dev))
414 iterator(data, sdata->dev->dev_addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100415 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100416 }
Johannes Berge38bad42007-11-28 10:55:32 +0100417
418 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100419}
420EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
Luis Carlos Coboee385852008-02-23 15:17:11 +0100421
422#ifdef CONFIG_MAC80211_MESH
423/**
424 * ieee80211_new_mesh_header - create a new mesh header
425 * @meshhdr: uninitialized mesh header
426 * @sdata: mesh interface to be used
427 *
428 * Return the header length.
429 */
430int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
431 struct ieee80211_sub_if_data *sdata)
432{
433 meshhdr->flags = 0;
434 meshhdr->ttl = sdata->u.sta.mshcfg.dot11MeshTTL;
435
436 meshhdr->seqnum[0] = sdata->u.sta.mesh_seqnum[0]++;
437 meshhdr->seqnum[1] = sdata->u.sta.mesh_seqnum[1];
438 meshhdr->seqnum[2] = sdata->u.sta.mesh_seqnum[2];
439
440 if (sdata->u.sta.mesh_seqnum[0] == 0) {
441 sdata->u.sta.mesh_seqnum[1]++;
442 if (sdata->u.sta.mesh_seqnum[1] == 0)
443 sdata->u.sta.mesh_seqnum[2]++;
444 }
445
446 return 5;
447}
448#endif