xref: /linux/net/wireless/util.c (revision a115bc070b1fc57ab23f3972401425927b5b465c)
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 			if (ieee80211_has_order(fc))
233 				hdrlen += IEEE80211_HT_CTL_LEN;
234 		}
235 		goto out;
236 	}
237 
238 	if (ieee80211_is_ctl(fc)) {
239 		/*
240 		 * ACK and CTS are 10 bytes, all others 16. To see how
241 		 * to get this condition consider
242 		 *   subtype mask:   0b0000000011110000 (0x00F0)
243 		 *   ACK subtype:    0b0000000011010000 (0x00D0)
244 		 *   CTS subtype:    0b0000000011000000 (0x00C0)
245 		 *   bits that matter:         ^^^      (0x00E0)
246 		 *   value of those: 0b0000000011000000 (0x00C0)
247 		 */
248 		if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
249 			hdrlen = 10;
250 		else
251 			hdrlen = 16;
252 	}
253 out:
254 	return hdrlen;
255 }
256 EXPORT_SYMBOL(ieee80211_hdrlen);
257 
258 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
259 {
260 	const struct ieee80211_hdr *hdr =
261 			(const struct ieee80211_hdr *)skb->data;
262 	unsigned int hdrlen;
263 
264 	if (unlikely(skb->len < 10))
265 		return 0;
266 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
267 	if (unlikely(hdrlen > skb->len))
268 		return 0;
269 	return hdrlen;
270 }
271 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
272 
273 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
274 {
275 	int ae = meshhdr->flags & MESH_FLAGS_AE;
276 	/* 7.1.3.5a.2 */
277 	switch (ae) {
278 	case 0:
279 		return 6;
280 	case MESH_FLAGS_AE_A4:
281 		return 12;
282 	case MESH_FLAGS_AE_A5_A6:
283 		return 18;
284 	case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
285 		return 24;
286 	default:
287 		return 6;
288 	}
289 }
290 
291 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
292 			   enum nl80211_iftype iftype)
293 {
294 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
295 	u16 hdrlen, ethertype;
296 	u8 *payload;
297 	u8 dst[ETH_ALEN];
298 	u8 src[ETH_ALEN] __aligned(2);
299 
300 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
301 		return -1;
302 
303 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
304 
305 	/* convert IEEE 802.11 header + possible LLC headers into Ethernet
306 	 * header
307 	 * IEEE 802.11 address fields:
308 	 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
309 	 *   0     0   DA    SA    BSSID n/a
310 	 *   0     1   DA    BSSID SA    n/a
311 	 *   1     0   BSSID SA    DA    n/a
312 	 *   1     1   RA    TA    DA    SA
313 	 */
314 	memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
315 	memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
316 
317 	switch (hdr->frame_control &
318 		cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
319 	case cpu_to_le16(IEEE80211_FCTL_TODS):
320 		if (unlikely(iftype != NL80211_IFTYPE_AP &&
321 			     iftype != NL80211_IFTYPE_AP_VLAN))
322 			return -1;
323 		break;
324 	case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
325 		if (unlikely(iftype != NL80211_IFTYPE_WDS &&
326 			     iftype != NL80211_IFTYPE_MESH_POINT &&
327 			     iftype != NL80211_IFTYPE_AP_VLAN &&
328 			     iftype != NL80211_IFTYPE_STATION))
329 			return -1;
330 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
331 			struct ieee80211s_hdr *meshdr =
332 				(struct ieee80211s_hdr *) (skb->data + hdrlen);
333 			hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
334 			if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
335 				memcpy(dst, meshdr->eaddr1, ETH_ALEN);
336 				memcpy(src, meshdr->eaddr2, ETH_ALEN);
337 			}
338 		}
339 		break;
340 	case cpu_to_le16(IEEE80211_FCTL_FROMDS):
341 		if ((iftype != NL80211_IFTYPE_STATION &&
342 		    iftype != NL80211_IFTYPE_MESH_POINT) ||
343 		    (is_multicast_ether_addr(dst) &&
344 		     !compare_ether_addr(src, addr)))
345 			return -1;
346 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
347 			struct ieee80211s_hdr *meshdr =
348 				(struct ieee80211s_hdr *) (skb->data + hdrlen);
349 			hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
350 			if (meshdr->flags & MESH_FLAGS_AE_A4)
351 				memcpy(src, meshdr->eaddr1, ETH_ALEN);
352 		}
353 		break;
354 	case cpu_to_le16(0):
355 		if (iftype != NL80211_IFTYPE_ADHOC)
356 			return -1;
357 		break;
358 	}
359 
360 	if (unlikely(skb->len - hdrlen < 8))
361 		return -1;
362 
363 	payload = skb->data + hdrlen;
364 	ethertype = (payload[6] << 8) | payload[7];
365 
366 	if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
367 		    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
368 		   compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
369 		/* remove RFC1042 or Bridge-Tunnel encapsulation and
370 		 * replace EtherType */
371 		skb_pull(skb, hdrlen + 6);
372 		memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
373 		memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
374 	} else {
375 		struct ethhdr *ehdr;
376 		__be16 len;
377 
378 		skb_pull(skb, hdrlen);
379 		len = htons(skb->len);
380 		ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
381 		memcpy(ehdr->h_dest, dst, ETH_ALEN);
382 		memcpy(ehdr->h_source, src, ETH_ALEN);
383 		ehdr->h_proto = len;
384 	}
385 	return 0;
386 }
387 EXPORT_SYMBOL(ieee80211_data_to_8023);
388 
389 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
390 			     enum nl80211_iftype iftype, u8 *bssid, bool qos)
391 {
392 	struct ieee80211_hdr hdr;
393 	u16 hdrlen, ethertype;
394 	__le16 fc;
395 	const u8 *encaps_data;
396 	int encaps_len, skip_header_bytes;
397 	int nh_pos, h_pos;
398 	int head_need;
399 
400 	if (unlikely(skb->len < ETH_HLEN))
401 		return -EINVAL;
402 
403 	nh_pos = skb_network_header(skb) - skb->data;
404 	h_pos = skb_transport_header(skb) - skb->data;
405 
406 	/* convert Ethernet header to proper 802.11 header (based on
407 	 * operation mode) */
408 	ethertype = (skb->data[12] << 8) | skb->data[13];
409 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
410 
411 	switch (iftype) {
412 	case NL80211_IFTYPE_AP:
413 	case NL80211_IFTYPE_AP_VLAN:
414 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
415 		/* DA BSSID SA */
416 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
417 		memcpy(hdr.addr2, addr, ETH_ALEN);
418 		memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
419 		hdrlen = 24;
420 		break;
421 	case NL80211_IFTYPE_STATION:
422 		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
423 		/* BSSID SA DA */
424 		memcpy(hdr.addr1, bssid, ETH_ALEN);
425 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
426 		memcpy(hdr.addr3, skb->data, ETH_ALEN);
427 		hdrlen = 24;
428 		break;
429 	case NL80211_IFTYPE_ADHOC:
430 		/* DA SA BSSID */
431 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
432 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
433 		memcpy(hdr.addr3, bssid, ETH_ALEN);
434 		hdrlen = 24;
435 		break;
436 	default:
437 		return -EOPNOTSUPP;
438 	}
439 
440 	if (qos) {
441 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
442 		hdrlen += 2;
443 	}
444 
445 	hdr.frame_control = fc;
446 	hdr.duration_id = 0;
447 	hdr.seq_ctrl = 0;
448 
449 	skip_header_bytes = ETH_HLEN;
450 	if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
451 		encaps_data = bridge_tunnel_header;
452 		encaps_len = sizeof(bridge_tunnel_header);
453 		skip_header_bytes -= 2;
454 	} else if (ethertype > 0x600) {
455 		encaps_data = rfc1042_header;
456 		encaps_len = sizeof(rfc1042_header);
457 		skip_header_bytes -= 2;
458 	} else {
459 		encaps_data = NULL;
460 		encaps_len = 0;
461 	}
462 
463 	skb_pull(skb, skip_header_bytes);
464 	nh_pos -= skip_header_bytes;
465 	h_pos -= skip_header_bytes;
466 
467 	head_need = hdrlen + encaps_len - skb_headroom(skb);
468 
469 	if (head_need > 0 || skb_cloned(skb)) {
470 		head_need = max(head_need, 0);
471 		if (head_need)
472 			skb_orphan(skb);
473 
474 		if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
475 			printk(KERN_ERR "failed to reallocate Tx buffer\n");
476 			return -ENOMEM;
477 		}
478 		skb->truesize += head_need;
479 	}
480 
481 	if (encaps_data) {
482 		memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
483 		nh_pos += encaps_len;
484 		h_pos += encaps_len;
485 	}
486 
487 	memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
488 
489 	nh_pos += hdrlen;
490 	h_pos += hdrlen;
491 
492 	/* Update skb pointers to various headers since this modified frame
493 	 * is going to go through Linux networking code that may potentially
494 	 * need things like pointer to IP header. */
495 	skb_set_mac_header(skb, 0);
496 	skb_set_network_header(skb, nh_pos);
497 	skb_set_transport_header(skb, h_pos);
498 
499 	return 0;
500 }
501 EXPORT_SYMBOL(ieee80211_data_from_8023);
502 
503 
504 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
505 			      const u8 *addr, enum nl80211_iftype iftype,
506 			      const unsigned int extra_headroom)
507 {
508 	struct sk_buff *frame = NULL;
509 	u16 ethertype;
510 	u8 *payload;
511 	const struct ethhdr *eth;
512 	int remaining, err;
513 	u8 dst[ETH_ALEN], src[ETH_ALEN];
514 
515 	err = ieee80211_data_to_8023(skb, addr, iftype);
516 	if (err)
517 		goto out;
518 
519 	/* skip the wrapping header */
520 	eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
521 	if (!eth)
522 		goto out;
523 
524 	while (skb != frame) {
525 		u8 padding;
526 		__be16 len = eth->h_proto;
527 		unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
528 
529 		remaining = skb->len;
530 		memcpy(dst, eth->h_dest, ETH_ALEN);
531 		memcpy(src, eth->h_source, ETH_ALEN);
532 
533 		padding = (4 - subframe_len) & 0x3;
534 		/* the last MSDU has no padding */
535 		if (subframe_len > remaining)
536 			goto purge;
537 
538 		skb_pull(skb, sizeof(struct ethhdr));
539 		/* reuse skb for the last subframe */
540 		if (remaining <= subframe_len + padding)
541 			frame = skb;
542 		else {
543 			unsigned int hlen = ALIGN(extra_headroom, 4);
544 			/*
545 			 * Allocate and reserve two bytes more for payload
546 			 * alignment since sizeof(struct ethhdr) is 14.
547 			 */
548 			frame = dev_alloc_skb(hlen + subframe_len + 2);
549 			if (!frame)
550 				goto purge;
551 
552 			skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
553 			memcpy(skb_put(frame, ntohs(len)), skb->data,
554 				ntohs(len));
555 
556 			eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
557 							padding);
558 			if (!eth) {
559 				dev_kfree_skb(frame);
560 				goto purge;
561 			}
562 		}
563 
564 		skb_reset_network_header(frame);
565 		frame->dev = skb->dev;
566 		frame->priority = skb->priority;
567 
568 		payload = frame->data;
569 		ethertype = (payload[6] << 8) | payload[7];
570 
571 		if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
572 			    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
573 			   compare_ether_addr(payload,
574 					      bridge_tunnel_header) == 0)) {
575 			/* remove RFC1042 or Bridge-Tunnel
576 			 * encapsulation and replace EtherType */
577 			skb_pull(frame, 6);
578 			memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
579 			memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
580 		} else {
581 			memcpy(skb_push(frame, sizeof(__be16)), &len,
582 				sizeof(__be16));
583 			memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
584 			memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
585 		}
586 		__skb_queue_tail(list, frame);
587 	}
588 
589 	return;
590 
591  purge:
592 	__skb_queue_purge(list);
593  out:
594 	dev_kfree_skb(skb);
595 }
596 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
597 
598 /* Given a data frame determine the 802.1p/1d tag to use. */
599 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
600 {
601 	unsigned int dscp;
602 
603 	/* skb->priority values from 256->263 are magic values to
604 	 * directly indicate a specific 802.1d priority.  This is used
605 	 * to allow 802.1d priority to be passed directly in from VLAN
606 	 * tags, etc.
607 	 */
608 	if (skb->priority >= 256 && skb->priority <= 263)
609 		return skb->priority - 256;
610 
611 	switch (skb->protocol) {
612 	case htons(ETH_P_IP):
613 		dscp = ip_hdr(skb)->tos & 0xfc;
614 		break;
615 	default:
616 		return 0;
617 	}
618 
619 	return dscp >> 5;
620 }
621 EXPORT_SYMBOL(cfg80211_classify8021d);
622 
623 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
624 {
625 	u8 *end, *pos;
626 
627 	pos = bss->information_elements;
628 	if (pos == NULL)
629 		return NULL;
630 	end = pos + bss->len_information_elements;
631 
632 	while (pos + 1 < end) {
633 		if (pos + 2 + pos[1] > end)
634 			break;
635 		if (pos[0] == ie)
636 			return pos;
637 		pos += 2 + pos[1];
638 	}
639 
640 	return NULL;
641 }
642 EXPORT_SYMBOL(ieee80211_bss_get_ie);
643 
644 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
645 {
646 	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
647 	struct net_device *dev = wdev->netdev;
648 	int i;
649 
650 	if (!wdev->connect_keys)
651 		return;
652 
653 	for (i = 0; i < 6; i++) {
654 		if (!wdev->connect_keys->params[i].cipher)
655 			continue;
656 		if (rdev->ops->add_key(wdev->wiphy, dev, i, NULL,
657 					&wdev->connect_keys->params[i])) {
658 			printk(KERN_ERR "%s: failed to set key %d\n",
659 				dev->name, i);
660 			continue;
661 		}
662 		if (wdev->connect_keys->def == i)
663 			if (rdev->ops->set_default_key(wdev->wiphy, dev, i)) {
664 				printk(KERN_ERR "%s: failed to set defkey %d\n",
665 					dev->name, i);
666 				continue;
667 			}
668 		if (wdev->connect_keys->defmgmt == i)
669 			if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
670 				printk(KERN_ERR "%s: failed to set mgtdef %d\n",
671 					dev->name, i);
672 	}
673 
674 	kfree(wdev->connect_keys);
675 	wdev->connect_keys = NULL;
676 }
677 
678 static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
679 {
680 	struct cfg80211_event *ev;
681 	unsigned long flags;
682 	const u8 *bssid = NULL;
683 
684 	spin_lock_irqsave(&wdev->event_lock, flags);
685 	while (!list_empty(&wdev->event_list)) {
686 		ev = list_first_entry(&wdev->event_list,
687 				      struct cfg80211_event, list);
688 		list_del(&ev->list);
689 		spin_unlock_irqrestore(&wdev->event_lock, flags);
690 
691 		wdev_lock(wdev);
692 		switch (ev->type) {
693 		case EVENT_CONNECT_RESULT:
694 			if (!is_zero_ether_addr(ev->cr.bssid))
695 				bssid = ev->cr.bssid;
696 			__cfg80211_connect_result(
697 				wdev->netdev, bssid,
698 				ev->cr.req_ie, ev->cr.req_ie_len,
699 				ev->cr.resp_ie, ev->cr.resp_ie_len,
700 				ev->cr.status,
701 				ev->cr.status == WLAN_STATUS_SUCCESS,
702 				NULL);
703 			break;
704 		case EVENT_ROAMED:
705 			__cfg80211_roamed(wdev, ev->rm.bssid,
706 					  ev->rm.req_ie, ev->rm.req_ie_len,
707 					  ev->rm.resp_ie, ev->rm.resp_ie_len);
708 			break;
709 		case EVENT_DISCONNECTED:
710 			__cfg80211_disconnected(wdev->netdev,
711 						ev->dc.ie, ev->dc.ie_len,
712 						ev->dc.reason, true);
713 			break;
714 		case EVENT_IBSS_JOINED:
715 			__cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
716 			break;
717 		}
718 		wdev_unlock(wdev);
719 
720 		kfree(ev);
721 
722 		spin_lock_irqsave(&wdev->event_lock, flags);
723 	}
724 	spin_unlock_irqrestore(&wdev->event_lock, flags);
725 }
726 
727 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
728 {
729 	struct wireless_dev *wdev;
730 
731 	ASSERT_RTNL();
732 	ASSERT_RDEV_LOCK(rdev);
733 
734 	mutex_lock(&rdev->devlist_mtx);
735 
736 	list_for_each_entry(wdev, &rdev->netdev_list, list)
737 		cfg80211_process_wdev_events(wdev);
738 
739 	mutex_unlock(&rdev->devlist_mtx);
740 }
741 
742 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
743 			  struct net_device *dev, enum nl80211_iftype ntype,
744 			  u32 *flags, struct vif_params *params)
745 {
746 	int err;
747 	enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
748 
749 	ASSERT_RDEV_LOCK(rdev);
750 
751 	/* don't support changing VLANs, you just re-create them */
752 	if (otype == NL80211_IFTYPE_AP_VLAN)
753 		return -EOPNOTSUPP;
754 
755 	if (!rdev->ops->change_virtual_intf ||
756 	    !(rdev->wiphy.interface_modes & (1 << ntype)))
757 		return -EOPNOTSUPP;
758 
759 	/* if it's part of a bridge, reject changing type to station/ibss */
760 	if (dev->br_port && (ntype == NL80211_IFTYPE_ADHOC ||
761 			     ntype == NL80211_IFTYPE_STATION))
762 		return -EBUSY;
763 
764 	if (ntype != otype) {
765 		dev->ieee80211_ptr->use_4addr = false;
766 
767 		switch (otype) {
768 		case NL80211_IFTYPE_ADHOC:
769 			cfg80211_leave_ibss(rdev, dev, false);
770 			break;
771 		case NL80211_IFTYPE_STATION:
772 			cfg80211_disconnect(rdev, dev,
773 					    WLAN_REASON_DEAUTH_LEAVING, true);
774 			break;
775 		case NL80211_IFTYPE_MESH_POINT:
776 			/* mesh should be handled? */
777 			break;
778 		default:
779 			break;
780 		}
781 
782 		cfg80211_process_rdev_events(rdev);
783 	}
784 
785 	err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
786 					     ntype, flags, params);
787 
788 	WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
789 
790 	if (!err && params && params->use_4addr != -1)
791 		dev->ieee80211_ptr->use_4addr = params->use_4addr;
792 
793 	if (!err) {
794 		dev->priv_flags &= ~IFF_DONT_BRIDGE;
795 		switch (ntype) {
796 		case NL80211_IFTYPE_STATION:
797 			if (dev->ieee80211_ptr->use_4addr)
798 				break;
799 			/* fall through */
800 		case NL80211_IFTYPE_ADHOC:
801 			dev->priv_flags |= IFF_DONT_BRIDGE;
802 			break;
803 		case NL80211_IFTYPE_AP:
804 		case NL80211_IFTYPE_AP_VLAN:
805 		case NL80211_IFTYPE_WDS:
806 		case NL80211_IFTYPE_MESH_POINT:
807 			/* bridging OK */
808 			break;
809 		case NL80211_IFTYPE_MONITOR:
810 			/* monitor can't bridge anyway */
811 			break;
812 		case NL80211_IFTYPE_UNSPECIFIED:
813 		case __NL80211_IFTYPE_AFTER_LAST:
814 			/* not happening */
815 			break;
816 		}
817 	}
818 
819 	return err;
820 }
821 
822 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
823 {
824 	int modulation, streams, bitrate;
825 
826 	if (!(rate->flags & RATE_INFO_FLAGS_MCS))
827 		return rate->legacy;
828 
829 	/* the formula below does only work for MCS values smaller than 32 */
830 	if (rate->mcs >= 32)
831 		return 0;
832 
833 	modulation = rate->mcs & 7;
834 	streams = (rate->mcs >> 3) + 1;
835 
836 	bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
837 			13500000 : 6500000;
838 
839 	if (modulation < 4)
840 		bitrate *= (modulation + 1);
841 	else if (modulation == 4)
842 		bitrate *= (modulation + 2);
843 	else
844 		bitrate *= (modulation + 3);
845 
846 	bitrate *= streams;
847 
848 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
849 		bitrate = (bitrate / 9) * 10;
850 
851 	/* do NOT round down here */
852 	return (bitrate + 50000) / 100000;
853 }
854