mac80211/cfg80211: add station events
[safe/jmp/linux-2.6] / net / wireless / util.c
1 /*
2  * Wireless utility functions
3  *
4  * Copyright 2007-2009  Johannes Berg <johannes@sipsolutions.net>
5  */
6 #include <linux/bitops.h>
7 #include <linux/etherdevice.h>
8 #include <net/cfg80211.h>
9 #include <net/ip.h>
10 #include "core.h"
11
12 struct ieee80211_rate *
13 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
14                             u32 basic_rates, int bitrate)
15 {
16         struct ieee80211_rate *result = &sband->bitrates[0];
17         int i;
18
19         for (i = 0; i < sband->n_bitrates; i++) {
20                 if (!(basic_rates & BIT(i)))
21                         continue;
22                 if (sband->bitrates[i].bitrate > bitrate)
23                         continue;
24                 result = &sband->bitrates[i];
25         }
26
27         return result;
28 }
29 EXPORT_SYMBOL(ieee80211_get_response_rate);
30
31 int ieee80211_channel_to_frequency(int chan)
32 {
33         if (chan < 14)
34                 return 2407 + chan * 5;
35
36         if (chan == 14)
37                 return 2484;
38
39         /* FIXME: 802.11j 17.3.8.3.2 */
40         return (chan + 1000) * 5;
41 }
42 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
43
44 int ieee80211_frequency_to_channel(int freq)
45 {
46         if (freq == 2484)
47                 return 14;
48
49         if (freq < 2484)
50                 return (freq - 2407) / 5;
51
52         /* FIXME: 802.11j 17.3.8.3.2 */
53         return freq/5 - 1000;
54 }
55 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
56
57 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
58                                                   int freq)
59 {
60         enum ieee80211_band band;
61         struct ieee80211_supported_band *sband;
62         int i;
63
64         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
65                 sband = wiphy->bands[band];
66
67                 if (!sband)
68                         continue;
69
70                 for (i = 0; i < sband->n_channels; i++) {
71                         if (sband->channels[i].center_freq == freq)
72                                 return &sband->channels[i];
73                 }
74         }
75
76         return NULL;
77 }
78 EXPORT_SYMBOL(__ieee80211_get_channel);
79
80 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
81                                      enum ieee80211_band band)
82 {
83         int i, want;
84
85         switch (band) {
86         case IEEE80211_BAND_5GHZ:
87                 want = 3;
88                 for (i = 0; i < sband->n_bitrates; i++) {
89                         if (sband->bitrates[i].bitrate == 60 ||
90                             sband->bitrates[i].bitrate == 120 ||
91                             sband->bitrates[i].bitrate == 240) {
92                                 sband->bitrates[i].flags |=
93                                         IEEE80211_RATE_MANDATORY_A;
94                                 want--;
95                         }
96                 }
97                 WARN_ON(want);
98                 break;
99         case IEEE80211_BAND_2GHZ:
100                 want = 7;
101                 for (i = 0; i < sband->n_bitrates; i++) {
102                         if (sband->bitrates[i].bitrate == 10) {
103                                 sband->bitrates[i].flags |=
104                                         IEEE80211_RATE_MANDATORY_B |
105                                         IEEE80211_RATE_MANDATORY_G;
106                                 want--;
107                         }
108
109                         if (sband->bitrates[i].bitrate == 20 ||
110                             sband->bitrates[i].bitrate == 55 ||
111                             sband->bitrates[i].bitrate == 110 ||
112                             sband->bitrates[i].bitrate == 60 ||
113                             sband->bitrates[i].bitrate == 120 ||
114                             sband->bitrates[i].bitrate == 240) {
115                                 sband->bitrates[i].flags |=
116                                         IEEE80211_RATE_MANDATORY_G;
117                                 want--;
118                         }
119
120                         if (sband->bitrates[i].bitrate != 10 &&
121                             sband->bitrates[i].bitrate != 20 &&
122                             sband->bitrates[i].bitrate != 55 &&
123                             sband->bitrates[i].bitrate != 110)
124                                 sband->bitrates[i].flags |=
125                                         IEEE80211_RATE_ERP_G;
126                 }
127                 WARN_ON(want != 0 && want != 3 && want != 6);
128                 break;
129         case IEEE80211_NUM_BANDS:
130                 WARN_ON(1);
131                 break;
132         }
133 }
134
135 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
136 {
137         enum ieee80211_band band;
138
139         for (band = 0; band < IEEE80211_NUM_BANDS; band++)
140                 if (wiphy->bands[band])
141                         set_mandatory_flags_band(wiphy->bands[band], band);
142 }
143
144 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
145                                    struct key_params *params, int key_idx,
146                                    const u8 *mac_addr)
147 {
148         int i;
149
150         if (key_idx > 5)
151                 return -EINVAL;
152
153         /*
154          * Disallow pairwise keys with non-zero index unless it's WEP
155          * (because current deployments use pairwise WEP keys with
156          * non-zero indizes but 802.11i clearly specifies to use zero)
157          */
158         if (mac_addr && key_idx &&
159             params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
160             params->cipher != WLAN_CIPHER_SUITE_WEP104)
161                 return -EINVAL;
162
163         switch (params->cipher) {
164         case WLAN_CIPHER_SUITE_WEP40:
165                 if (params->key_len != WLAN_KEY_LEN_WEP40)
166                         return -EINVAL;
167                 break;
168         case WLAN_CIPHER_SUITE_TKIP:
169                 if (params->key_len != WLAN_KEY_LEN_TKIP)
170                         return -EINVAL;
171                 break;
172         case WLAN_CIPHER_SUITE_CCMP:
173                 if (params->key_len != WLAN_KEY_LEN_CCMP)
174                         return -EINVAL;
175                 break;
176         case WLAN_CIPHER_SUITE_WEP104:
177                 if (params->key_len != WLAN_KEY_LEN_WEP104)
178                         return -EINVAL;
179                 break;
180         case WLAN_CIPHER_SUITE_AES_CMAC:
181                 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
182                         return -EINVAL;
183                 break;
184         default:
185                 return -EINVAL;
186         }
187
188         if (params->seq) {
189                 switch (params->cipher) {
190                 case WLAN_CIPHER_SUITE_WEP40:
191                 case WLAN_CIPHER_SUITE_WEP104:
192                         /* These ciphers do not use key sequence */
193                         return -EINVAL;
194                 case WLAN_CIPHER_SUITE_TKIP:
195                 case WLAN_CIPHER_SUITE_CCMP:
196                 case WLAN_CIPHER_SUITE_AES_CMAC:
197                         if (params->seq_len != 6)
198                                 return -EINVAL;
199                         break;
200                 }
201         }
202
203         for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
204                 if (params->cipher == rdev->wiphy.cipher_suites[i])
205                         break;
206         if (i == rdev->wiphy.n_cipher_suites)
207                 return -EINVAL;
208
209         return 0;
210 }
211
212 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
213 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
214 const unsigned char rfc1042_header[] __aligned(2) =
215         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
216 EXPORT_SYMBOL(rfc1042_header);
217
218 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
219 const unsigned char bridge_tunnel_header[] __aligned(2) =
220         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
221 EXPORT_SYMBOL(bridge_tunnel_header);
222
223 unsigned int ieee80211_hdrlen(__le16 fc)
224 {
225         unsigned int hdrlen = 24;
226
227         if (ieee80211_is_data(fc)) {
228                 if (ieee80211_has_a4(fc))
229                         hdrlen = 30;
230                 if (ieee80211_is_data_qos(fc))
231                         hdrlen += IEEE80211_QOS_CTL_LEN;
232                 goto out;
233         }
234
235         if (ieee80211_is_ctl(fc)) {
236                 /*
237                  * ACK and CTS are 10 bytes, all others 16. To see how
238                  * to get this condition consider
239                  *   subtype mask:   0b0000000011110000 (0x00F0)
240                  *   ACK subtype:    0b0000000011010000 (0x00D0)
241                  *   CTS subtype:    0b0000000011000000 (0x00C0)
242                  *   bits that matter:         ^^^      (0x00E0)
243                  *   value of those: 0b0000000011000000 (0x00C0)
244                  */
245                 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
246                         hdrlen = 10;
247                 else
248                         hdrlen = 16;
249         }
250 out:
251         return hdrlen;
252 }
253 EXPORT_SYMBOL(ieee80211_hdrlen);
254
255 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
256 {
257         const struct ieee80211_hdr *hdr =
258                         (const struct ieee80211_hdr *)skb->data;
259         unsigned int hdrlen;
260
261         if (unlikely(skb->len < 10))
262                 return 0;
263         hdrlen = ieee80211_hdrlen(hdr->frame_control);
264         if (unlikely(hdrlen > skb->len))
265                 return 0;
266         return hdrlen;
267 }
268 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
269
270 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
271 {
272         int ae = meshhdr->flags & MESH_FLAGS_AE;
273         /* 7.1.3.5a.2 */
274         switch (ae) {
275         case 0:
276                 return 6;
277         case MESH_FLAGS_AE_A4:
278                 return 12;
279         case MESH_FLAGS_AE_A5_A6:
280                 return 18;
281         case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
282                 return 24;
283         default:
284                 return 6;
285         }
286 }
287
288 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
289                            enum nl80211_iftype iftype)
290 {
291         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
292         u16 hdrlen, ethertype;
293         u8 *payload;
294         u8 dst[ETH_ALEN];
295         u8 src[ETH_ALEN] __aligned(2);
296
297         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
298                 return -1;
299
300         hdrlen = ieee80211_hdrlen(hdr->frame_control);
301
302         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
303          * header
304          * IEEE 802.11 address fields:
305          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
306          *   0     0   DA    SA    BSSID n/a
307          *   0     1   DA    BSSID SA    n/a
308          *   1     0   BSSID SA    DA    n/a
309          *   1     1   RA    TA    DA    SA
310          */
311         memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
312         memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
313
314         switch (hdr->frame_control &
315                 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
316         case cpu_to_le16(IEEE80211_FCTL_TODS):
317                 if (unlikely(iftype != NL80211_IFTYPE_AP &&
318                              iftype != NL80211_IFTYPE_AP_VLAN))
319                         return -1;
320                 break;
321         case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
322                 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
323                              iftype != NL80211_IFTYPE_MESH_POINT &&
324                              iftype != NL80211_IFTYPE_AP_VLAN &&
325                              iftype != NL80211_IFTYPE_STATION))
326                         return -1;
327                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
328                         struct ieee80211s_hdr *meshdr =
329                                 (struct ieee80211s_hdr *) (skb->data + hdrlen);
330                         hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
331                         if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
332                                 memcpy(dst, meshdr->eaddr1, ETH_ALEN);
333                                 memcpy(src, meshdr->eaddr2, ETH_ALEN);
334                         }
335                 }
336                 break;
337         case cpu_to_le16(IEEE80211_FCTL_FROMDS):
338                 if ((iftype != NL80211_IFTYPE_STATION &&
339                     iftype != NL80211_IFTYPE_MESH_POINT) ||
340                     (is_multicast_ether_addr(dst) &&
341                      !compare_ether_addr(src, addr)))
342                         return -1;
343                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
344                         struct ieee80211s_hdr *meshdr =
345                                 (struct ieee80211s_hdr *) (skb->data + hdrlen);
346                         hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
347                         if (meshdr->flags & MESH_FLAGS_AE_A4)
348                                 memcpy(src, meshdr->eaddr1, ETH_ALEN);
349                 }
350                 break;
351         case cpu_to_le16(0):
352                 if (iftype != NL80211_IFTYPE_ADHOC)
353                         return -1;
354                 break;
355         }
356
357         if (unlikely(skb->len - hdrlen < 8))
358                 return -1;
359
360         payload = skb->data + hdrlen;
361         ethertype = (payload[6] << 8) | payload[7];
362
363         if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
364                     ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
365                    compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
366                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
367                  * replace EtherType */
368                 skb_pull(skb, hdrlen + 6);
369                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
370                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
371         } else {
372                 struct ethhdr *ehdr;
373                 __be16 len;
374
375                 skb_pull(skb, hdrlen);
376                 len = htons(skb->len);
377                 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
378                 memcpy(ehdr->h_dest, dst, ETH_ALEN);
379                 memcpy(ehdr->h_source, src, ETH_ALEN);
380                 ehdr->h_proto = len;
381         }
382         return 0;
383 }
384 EXPORT_SYMBOL(ieee80211_data_to_8023);
385
386 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
387                              enum nl80211_iftype iftype, u8 *bssid, bool qos)
388 {
389         struct ieee80211_hdr hdr;
390         u16 hdrlen, ethertype;
391         __le16 fc;
392         const u8 *encaps_data;
393         int encaps_len, skip_header_bytes;
394         int nh_pos, h_pos;
395         int head_need;
396
397         if (unlikely(skb->len < ETH_HLEN))
398                 return -EINVAL;
399
400         nh_pos = skb_network_header(skb) - skb->data;
401         h_pos = skb_transport_header(skb) - skb->data;
402
403         /* convert Ethernet header to proper 802.11 header (based on
404          * operation mode) */
405         ethertype = (skb->data[12] << 8) | skb->data[13];
406         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
407
408         switch (iftype) {
409         case NL80211_IFTYPE_AP:
410         case NL80211_IFTYPE_AP_VLAN:
411                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
412                 /* DA BSSID SA */
413                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
414                 memcpy(hdr.addr2, addr, ETH_ALEN);
415                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
416                 hdrlen = 24;
417                 break;
418         case NL80211_IFTYPE_STATION:
419                 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
420                 /* BSSID SA DA */
421                 memcpy(hdr.addr1, bssid, ETH_ALEN);
422                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
423                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
424                 hdrlen = 24;
425                 break;
426         case NL80211_IFTYPE_ADHOC:
427                 /* DA SA BSSID */
428                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
429                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
430                 memcpy(hdr.addr3, bssid, ETH_ALEN);
431                 hdrlen = 24;
432                 break;
433         default:
434                 return -EOPNOTSUPP;
435         }
436
437         if (qos) {
438                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
439                 hdrlen += 2;
440         }
441
442         hdr.frame_control = fc;
443         hdr.duration_id = 0;
444         hdr.seq_ctrl = 0;
445
446         skip_header_bytes = ETH_HLEN;
447         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
448                 encaps_data = bridge_tunnel_header;
449                 encaps_len = sizeof(bridge_tunnel_header);
450                 skip_header_bytes -= 2;
451         } else if (ethertype > 0x600) {
452                 encaps_data = rfc1042_header;
453                 encaps_len = sizeof(rfc1042_header);
454                 skip_header_bytes -= 2;
455         } else {
456                 encaps_data = NULL;
457                 encaps_len = 0;
458         }
459
460         skb_pull(skb, skip_header_bytes);
461         nh_pos -= skip_header_bytes;
462         h_pos -= skip_header_bytes;
463
464         head_need = hdrlen + encaps_len - skb_headroom(skb);
465
466         if (head_need > 0 || skb_cloned(skb)) {
467                 head_need = max(head_need, 0);
468                 if (head_need)
469                         skb_orphan(skb);
470
471                 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
472                         printk(KERN_ERR "failed to reallocate Tx buffer\n");
473                         return -ENOMEM;
474                 }
475                 skb->truesize += head_need;
476         }
477
478         if (encaps_data) {
479                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
480                 nh_pos += encaps_len;
481                 h_pos += encaps_len;
482         }
483
484         memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
485
486         nh_pos += hdrlen;
487         h_pos += hdrlen;
488
489         /* Update skb pointers to various headers since this modified frame
490          * is going to go through Linux networking code that may potentially
491          * need things like pointer to IP header. */
492         skb_set_mac_header(skb, 0);
493         skb_set_network_header(skb, nh_pos);
494         skb_set_transport_header(skb, h_pos);
495
496         return 0;
497 }
498 EXPORT_SYMBOL(ieee80211_data_from_8023);
499
500
501 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
502                               const u8 *addr, enum nl80211_iftype iftype,
503                               const unsigned int extra_headroom)
504 {
505         struct sk_buff *frame = NULL;
506         u16 ethertype;
507         u8 *payload;
508         const struct ethhdr *eth;
509         int remaining, err;
510         u8 dst[ETH_ALEN], src[ETH_ALEN];
511
512         err = ieee80211_data_to_8023(skb, addr, iftype);
513         if (err)
514                 goto out;
515
516         /* skip the wrapping header */
517         eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
518         if (!eth)
519                 goto out;
520
521         while (skb != frame) {
522                 u8 padding;
523                 __be16 len = eth->h_proto;
524                 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
525
526                 remaining = skb->len;
527                 memcpy(dst, eth->h_dest, ETH_ALEN);
528                 memcpy(src, eth->h_source, ETH_ALEN);
529
530                 padding = (4 - subframe_len) & 0x3;
531                 /* the last MSDU has no padding */
532                 if (subframe_len > remaining)
533                         goto purge;
534
535                 skb_pull(skb, sizeof(struct ethhdr));
536                 /* reuse skb for the last subframe */
537                 if (remaining <= subframe_len + padding)
538                         frame = skb;
539                 else {
540                         unsigned int hlen = ALIGN(extra_headroom, 4);
541                         /*
542                          * Allocate and reserve two bytes more for payload
543                          * alignment since sizeof(struct ethhdr) is 14.
544                          */
545                         frame = dev_alloc_skb(hlen + subframe_len + 2);
546                         if (!frame)
547                                 goto purge;
548
549                         skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
550                         memcpy(skb_put(frame, ntohs(len)), skb->data,
551                                 ntohs(len));
552
553                         eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
554                                                         padding);
555                         if (!eth) {
556                                 dev_kfree_skb(frame);
557                                 goto purge;
558                         }
559                 }
560
561                 skb_reset_network_header(frame);
562                 frame->dev = skb->dev;
563                 frame->priority = skb->priority;
564
565                 payload = frame->data;
566                 ethertype = (payload[6] << 8) | payload[7];
567
568                 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
569                             ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
570                            compare_ether_addr(payload,
571                                               bridge_tunnel_header) == 0)) {
572                         /* remove RFC1042 or Bridge-Tunnel
573                          * encapsulation and replace EtherType */
574                         skb_pull(frame, 6);
575                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
576                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
577                 } else {
578                         memcpy(skb_push(frame, sizeof(__be16)), &len,
579                                 sizeof(__be16));
580                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
581                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
582                 }
583                 __skb_queue_tail(list, frame);
584         }
585
586         return;
587
588  purge:
589         __skb_queue_purge(list);
590  out:
591         dev_kfree_skb(skb);
592 }
593 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
594
595 /* Given a data frame determine the 802.1p/1d tag to use. */
596 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
597 {
598         unsigned int dscp;
599
600         /* skb->priority values from 256->263 are magic values to
601          * directly indicate a specific 802.1d priority.  This is used
602          * to allow 802.1d priority to be passed directly in from VLAN
603          * tags, etc.
604          */
605         if (skb->priority >= 256 && skb->priority <= 263)
606                 return skb->priority - 256;
607
608         switch (skb->protocol) {
609         case htons(ETH_P_IP):
610                 dscp = ip_hdr(skb)->tos & 0xfc;
611                 break;
612         default:
613                 return 0;
614         }
615
616         return dscp >> 5;
617 }
618 EXPORT_SYMBOL(cfg80211_classify8021d);
619
620 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
621 {
622         u8 *end, *pos;
623
624         pos = bss->information_elements;
625         if (pos == NULL)
626                 return NULL;
627         end = pos + bss->len_information_elements;
628
629         while (pos + 1 < end) {
630                 if (pos + 2 + pos[1] > end)
631                         break;
632                 if (pos[0] == ie)
633                         return pos;
634                 pos += 2 + pos[1];
635         }
636
637         return NULL;
638 }
639 EXPORT_SYMBOL(ieee80211_bss_get_ie);
640
641 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
642 {
643         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
644         struct net_device *dev = wdev->netdev;
645         int i;
646
647         if (!wdev->connect_keys)
648                 return;
649
650         for (i = 0; i < 6; i++) {
651                 if (!wdev->connect_keys->params[i].cipher)
652                         continue;
653                 if (rdev->ops->add_key(wdev->wiphy, dev, i, NULL,
654                                         &wdev->connect_keys->params[i])) {
655                         printk(KERN_ERR "%s: failed to set key %d\n",
656                                 dev->name, i);
657                         continue;
658                 }
659                 if (wdev->connect_keys->def == i)
660                         if (rdev->ops->set_default_key(wdev->wiphy, dev, i)) {
661                                 printk(KERN_ERR "%s: failed to set defkey %d\n",
662                                         dev->name, i);
663                                 continue;
664                         }
665                 if (wdev->connect_keys->defmgmt == i)
666                         if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
667                                 printk(KERN_ERR "%s: failed to set mgtdef %d\n",
668                                         dev->name, i);
669         }
670
671         kfree(wdev->connect_keys);
672         wdev->connect_keys = NULL;
673 }
674
675 static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
676 {
677         struct cfg80211_event *ev;
678         unsigned long flags;
679         const u8 *bssid = NULL;
680
681         spin_lock_irqsave(&wdev->event_lock, flags);
682         while (!list_empty(&wdev->event_list)) {
683                 ev = list_first_entry(&wdev->event_list,
684                                       struct cfg80211_event, list);
685                 list_del(&ev->list);
686                 spin_unlock_irqrestore(&wdev->event_lock, flags);
687
688                 wdev_lock(wdev);
689                 switch (ev->type) {
690                 case EVENT_CONNECT_RESULT:
691                         if (!is_zero_ether_addr(ev->cr.bssid))
692                                 bssid = ev->cr.bssid;
693                         __cfg80211_connect_result(
694                                 wdev->netdev, bssid,
695                                 ev->cr.req_ie, ev->cr.req_ie_len,
696                                 ev->cr.resp_ie, ev->cr.resp_ie_len,
697                                 ev->cr.status,
698                                 ev->cr.status == WLAN_STATUS_SUCCESS,
699                                 NULL);
700                         break;
701                 case EVENT_ROAMED:
702                         __cfg80211_roamed(wdev, ev->rm.bssid,
703                                           ev->rm.req_ie, ev->rm.req_ie_len,
704                                           ev->rm.resp_ie, ev->rm.resp_ie_len);
705                         break;
706                 case EVENT_DISCONNECTED:
707                         __cfg80211_disconnected(wdev->netdev,
708                                                 ev->dc.ie, ev->dc.ie_len,
709                                                 ev->dc.reason, true);
710                         break;
711                 case EVENT_IBSS_JOINED:
712                         __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
713                         break;
714                 }
715                 wdev_unlock(wdev);
716
717                 kfree(ev);
718
719                 spin_lock_irqsave(&wdev->event_lock, flags);
720         }
721         spin_unlock_irqrestore(&wdev->event_lock, flags);
722 }
723
724 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
725 {
726         struct wireless_dev *wdev;
727
728         ASSERT_RTNL();
729         ASSERT_RDEV_LOCK(rdev);
730
731         mutex_lock(&rdev->devlist_mtx);
732
733         list_for_each_entry(wdev, &rdev->netdev_list, list)
734                 cfg80211_process_wdev_events(wdev);
735
736         mutex_unlock(&rdev->devlist_mtx);
737 }
738
739 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
740                           struct net_device *dev, enum nl80211_iftype ntype,
741                           u32 *flags, struct vif_params *params)
742 {
743         int err;
744         enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
745
746         ASSERT_RDEV_LOCK(rdev);
747
748         /* don't support changing VLANs, you just re-create them */
749         if (otype == NL80211_IFTYPE_AP_VLAN)
750                 return -EOPNOTSUPP;
751
752         if (!rdev->ops->change_virtual_intf ||
753             !(rdev->wiphy.interface_modes & (1 << ntype)))
754                 return -EOPNOTSUPP;
755
756         /* if it's part of a bridge, reject changing type to station/ibss */
757         if (dev->br_port && (ntype == NL80211_IFTYPE_ADHOC ||
758                              ntype == NL80211_IFTYPE_STATION))
759                 return -EBUSY;
760
761         if (ntype != otype) {
762                 dev->ieee80211_ptr->use_4addr = false;
763
764                 switch (otype) {
765                 case NL80211_IFTYPE_ADHOC:
766                         cfg80211_leave_ibss(rdev, dev, false);
767                         break;
768                 case NL80211_IFTYPE_STATION:
769                         cfg80211_disconnect(rdev, dev,
770                                             WLAN_REASON_DEAUTH_LEAVING, true);
771                         break;
772                 case NL80211_IFTYPE_MESH_POINT:
773                         /* mesh should be handled? */
774                         break;
775                 default:
776                         break;
777                 }
778
779                 cfg80211_process_rdev_events(rdev);
780         }
781
782         err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
783                                              ntype, flags, params);
784
785         WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
786
787         if (!err && params && params->use_4addr != -1)
788                 dev->ieee80211_ptr->use_4addr = params->use_4addr;
789
790         if (!err) {
791                 dev->priv_flags &= ~IFF_DONT_BRIDGE;
792                 switch (ntype) {
793                 case NL80211_IFTYPE_STATION:
794                         if (dev->ieee80211_ptr->use_4addr)
795                                 break;
796                         /* fall through */
797                 case NL80211_IFTYPE_ADHOC:
798                         dev->priv_flags |= IFF_DONT_BRIDGE;
799                         break;
800                 case NL80211_IFTYPE_AP:
801                 case NL80211_IFTYPE_AP_VLAN:
802                 case NL80211_IFTYPE_WDS:
803                 case NL80211_IFTYPE_MESH_POINT:
804                         /* bridging OK */
805                         break;
806                 case NL80211_IFTYPE_MONITOR:
807                         /* monitor can't bridge anyway */
808                         break;
809                 case NL80211_IFTYPE_UNSPECIFIED:
810                 case __NL80211_IFTYPE_AFTER_LAST:
811                         /* not happening */
812                         break;
813                 }
814         }
815
816         return err;
817 }
818
819 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
820 {
821         int modulation, streams, bitrate;
822
823         if (!(rate->flags & RATE_INFO_FLAGS_MCS))
824                 return rate->legacy;
825
826         /* the formula below does only work for MCS values smaller than 32 */
827         if (rate->mcs >= 32)
828                 return 0;
829
830         modulation = rate->mcs & 7;
831         streams = (rate->mcs >> 3) + 1;
832
833         bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
834                         13500000 : 6500000;
835
836         if (modulation < 4)
837                 bitrate *= (modulation + 1);
838         else if (modulation == 4)
839                 bitrate *= (modulation + 2);
840         else
841                 bitrate *= (modulation + 3);
842
843         bitrate *= streams;
844
845         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
846                 bitrate = (bitrate / 9) * 10;
847
848         /* do NOT round down here */
849         return (bitrate + 50000) / 100000;
850 }