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