1 /*
2  * Wireless utility functions
3  *
4  * Copyright 2007-2009	Johannes Berg <johannes@sipsolutions.net>
5  */
6 #include <linux/export.h>
7 #include <linux/bitops.h>
8 #include <linux/etherdevice.h>
9 #include <linux/slab.h>
10 #include <net/cfg80211.h>
11 #include <net/ip.h>
12 #include <net/dsfield.h>
13 #include "core.h"
14 
15 struct ieee80211_rate *
ieee80211_get_response_rate(struct ieee80211_supported_band * sband,u32 basic_rates,int bitrate)16 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
17 			    u32 basic_rates, int bitrate)
18 {
19 	struct ieee80211_rate *result = &sband->bitrates[0];
20 	int i;
21 
22 	for (i = 0; i < sband->n_bitrates; i++) {
23 		if (!(basic_rates & BIT(i)))
24 			continue;
25 		if (sband->bitrates[i].bitrate > bitrate)
26 			continue;
27 		result = &sband->bitrates[i];
28 	}
29 
30 	return result;
31 }
32 EXPORT_SYMBOL(ieee80211_get_response_rate);
33 
ieee80211_channel_to_frequency(int chan,enum ieee80211_band band)34 int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
35 {
36 	/* see 802.11 17.3.8.3.2 and Annex J
37 	 * there are overlapping channel numbers in 5GHz and 2GHz bands */
38 	if (band == IEEE80211_BAND_5GHZ) {
39 		if (chan >= 182 && chan <= 196)
40 			return 4000 + chan * 5;
41 		else
42 			return 5000 + chan * 5;
43 	} else { /* IEEE80211_BAND_2GHZ */
44 		if (chan == 14)
45 			return 2484;
46 		else if (chan < 14)
47 			return 2407 + chan * 5;
48 		else
49 			return 0; /* not supported */
50 	}
51 }
52 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
53 
ieee80211_frequency_to_channel(int freq)54 int ieee80211_frequency_to_channel(int freq)
55 {
56 	/* see 802.11 17.3.8.3.2 and Annex J */
57 	if (freq == 2484)
58 		return 14;
59 	else if (freq < 2484)
60 		return (freq - 2407) / 5;
61 	else if (freq >= 4910 && freq <= 4980)
62 		return (freq - 4000) / 5;
63 	else
64 		return (freq - 5000) / 5;
65 }
66 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
67 
__ieee80211_get_channel(struct wiphy * wiphy,int freq)68 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
69 						  int freq)
70 {
71 	enum ieee80211_band band;
72 	struct ieee80211_supported_band *sband;
73 	int i;
74 
75 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76 		sband = wiphy->bands[band];
77 
78 		if (!sband)
79 			continue;
80 
81 		for (i = 0; i < sband->n_channels; i++) {
82 			if (sband->channels[i].center_freq == freq)
83 				return &sband->channels[i];
84 		}
85 	}
86 
87 	return NULL;
88 }
89 EXPORT_SYMBOL(__ieee80211_get_channel);
90 
set_mandatory_flags_band(struct ieee80211_supported_band * sband,enum ieee80211_band band)91 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
92 				     enum ieee80211_band band)
93 {
94 	int i, want;
95 
96 	switch (band) {
97 	case IEEE80211_BAND_5GHZ:
98 		want = 3;
99 		for (i = 0; i < sband->n_bitrates; i++) {
100 			if (sband->bitrates[i].bitrate == 60 ||
101 			    sband->bitrates[i].bitrate == 120 ||
102 			    sband->bitrates[i].bitrate == 240) {
103 				sband->bitrates[i].flags |=
104 					IEEE80211_RATE_MANDATORY_A;
105 				want--;
106 			}
107 		}
108 		WARN_ON(want);
109 		break;
110 	case IEEE80211_BAND_2GHZ:
111 		want = 7;
112 		for (i = 0; i < sband->n_bitrates; i++) {
113 			if (sband->bitrates[i].bitrate == 10) {
114 				sband->bitrates[i].flags |=
115 					IEEE80211_RATE_MANDATORY_B |
116 					IEEE80211_RATE_MANDATORY_G;
117 				want--;
118 			}
119 
120 			if (sband->bitrates[i].bitrate == 20 ||
121 			    sband->bitrates[i].bitrate == 55 ||
122 			    sband->bitrates[i].bitrate == 110 ||
123 			    sband->bitrates[i].bitrate == 60 ||
124 			    sband->bitrates[i].bitrate == 120 ||
125 			    sband->bitrates[i].bitrate == 240) {
126 				sband->bitrates[i].flags |=
127 					IEEE80211_RATE_MANDATORY_G;
128 				want--;
129 			}
130 
131 			if (sband->bitrates[i].bitrate != 10 &&
132 			    sband->bitrates[i].bitrate != 20 &&
133 			    sband->bitrates[i].bitrate != 55 &&
134 			    sband->bitrates[i].bitrate != 110)
135 				sband->bitrates[i].flags |=
136 					IEEE80211_RATE_ERP_G;
137 		}
138 		WARN_ON(want != 0 && want != 3 && want != 6);
139 		break;
140 	case IEEE80211_NUM_BANDS:
141 		WARN_ON(1);
142 		break;
143 	}
144 }
145 
ieee80211_set_bitrate_flags(struct wiphy * wiphy)146 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
147 {
148 	enum ieee80211_band band;
149 
150 	for (band = 0; band < IEEE80211_NUM_BANDS; band++)
151 		if (wiphy->bands[band])
152 			set_mandatory_flags_band(wiphy->bands[band], band);
153 }
154 
cfg80211_supported_cipher_suite(struct wiphy * wiphy,u32 cipher)155 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
156 {
157 	int i;
158 	for (i = 0; i < wiphy->n_cipher_suites; i++)
159 		if (cipher == wiphy->cipher_suites[i])
160 			return true;
161 	return false;
162 }
163 
cfg80211_validate_key_settings(struct cfg80211_registered_device * rdev,struct key_params * params,int key_idx,bool pairwise,const u8 * mac_addr)164 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
165 				   struct key_params *params, int key_idx,
166 				   bool pairwise, const u8 *mac_addr)
167 {
168 	if (key_idx > 5)
169 		return -EINVAL;
170 
171 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
172 		return -EINVAL;
173 
174 	if (pairwise && !mac_addr)
175 		return -EINVAL;
176 
177 	/*
178 	 * Disallow pairwise keys with non-zero index unless it's WEP
179 	 * or a vendor specific cipher (because current deployments use
180 	 * pairwise WEP keys with non-zero indices and for vendor specific
181 	 * ciphers this should be validated in the driver or hardware level
182 	 * - but 802.11i clearly specifies to use zero)
183 	 */
184 	if (pairwise && key_idx &&
185 	    ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
186 	     (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
187 	     (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
188 		return -EINVAL;
189 
190 	switch (params->cipher) {
191 	case WLAN_CIPHER_SUITE_WEP40:
192 		if (params->key_len != WLAN_KEY_LEN_WEP40)
193 			return -EINVAL;
194 		break;
195 	case WLAN_CIPHER_SUITE_TKIP:
196 		if (params->key_len != WLAN_KEY_LEN_TKIP)
197 			return -EINVAL;
198 		break;
199 	case WLAN_CIPHER_SUITE_CCMP:
200 		if (params->key_len != WLAN_KEY_LEN_CCMP)
201 			return -EINVAL;
202 		break;
203 	case WLAN_CIPHER_SUITE_WEP104:
204 		if (params->key_len != WLAN_KEY_LEN_WEP104)
205 			return -EINVAL;
206 		break;
207 	case WLAN_CIPHER_SUITE_AES_CMAC:
208 		if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
209 			return -EINVAL;
210 		break;
211 	default:
212 		/*
213 		 * We don't know anything about this algorithm,
214 		 * allow using it -- but the driver must check
215 		 * all parameters! We still check below whether
216 		 * or not the driver supports this algorithm,
217 		 * of course.
218 		 */
219 		break;
220 	}
221 
222 	if (params->seq) {
223 		switch (params->cipher) {
224 		case WLAN_CIPHER_SUITE_WEP40:
225 		case WLAN_CIPHER_SUITE_WEP104:
226 			/* These ciphers do not use key sequence */
227 			return -EINVAL;
228 		case WLAN_CIPHER_SUITE_TKIP:
229 		case WLAN_CIPHER_SUITE_CCMP:
230 		case WLAN_CIPHER_SUITE_AES_CMAC:
231 			if (params->seq_len != 6)
232 				return -EINVAL;
233 			break;
234 		}
235 	}
236 
237 	if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
238 		return -EINVAL;
239 
240 	return 0;
241 }
242 
ieee80211_hdrlen(__le16 fc)243 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
244 {
245 	unsigned int hdrlen = 24;
246 
247 	if (ieee80211_is_data(fc)) {
248 		if (ieee80211_has_a4(fc))
249 			hdrlen = 30;
250 		if (ieee80211_is_data_qos(fc)) {
251 			hdrlen += IEEE80211_QOS_CTL_LEN;
252 			if (ieee80211_has_order(fc))
253 				hdrlen += IEEE80211_HT_CTL_LEN;
254 		}
255 		goto out;
256 	}
257 
258 	if (ieee80211_is_ctl(fc)) {
259 		/*
260 		 * ACK and CTS are 10 bytes, all others 16. To see how
261 		 * to get this condition consider
262 		 *   subtype mask:   0b0000000011110000 (0x00F0)
263 		 *   ACK subtype:    0b0000000011010000 (0x00D0)
264 		 *   CTS subtype:    0b0000000011000000 (0x00C0)
265 		 *   bits that matter:         ^^^      (0x00E0)
266 		 *   value of those: 0b0000000011000000 (0x00C0)
267 		 */
268 		if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
269 			hdrlen = 10;
270 		else
271 			hdrlen = 16;
272 	}
273 out:
274 	return hdrlen;
275 }
276 EXPORT_SYMBOL(ieee80211_hdrlen);
277 
ieee80211_get_hdrlen_from_skb(const struct sk_buff * skb)278 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
279 {
280 	const struct ieee80211_hdr *hdr =
281 			(const struct ieee80211_hdr *)skb->data;
282 	unsigned int hdrlen;
283 
284 	if (unlikely(skb->len < 10))
285 		return 0;
286 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
287 	if (unlikely(hdrlen > skb->len))
288 		return 0;
289 	return hdrlen;
290 }
291 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
292 
ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr * meshhdr)293 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
294 {
295 	int ae = meshhdr->flags & MESH_FLAGS_AE;
296 	/* 802.11-2012, 8.2.4.7.3 */
297 	switch (ae) {
298 	default:
299 	case 0:
300 		return 6;
301 	case MESH_FLAGS_AE_A4:
302 		return 12;
303 	case MESH_FLAGS_AE_A5_A6:
304 		return 18;
305 	}
306 }
307 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
308 
ieee80211_data_to_8023(struct sk_buff * skb,const u8 * addr,enum nl80211_iftype iftype)309 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
310 			   enum nl80211_iftype iftype)
311 {
312 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
313 	u16 hdrlen, ethertype;
314 	u8 *payload;
315 	u8 dst[ETH_ALEN];
316 	u8 src[ETH_ALEN] __aligned(2);
317 
318 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
319 		return -1;
320 
321 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
322 
323 	/* convert IEEE 802.11 header + possible LLC headers into Ethernet
324 	 * header
325 	 * IEEE 802.11 address fields:
326 	 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
327 	 *   0     0   DA    SA    BSSID n/a
328 	 *   0     1   DA    BSSID SA    n/a
329 	 *   1     0   BSSID SA    DA    n/a
330 	 *   1     1   RA    TA    DA    SA
331 	 */
332 	memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
333 	memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
334 
335 	switch (hdr->frame_control &
336 		cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
337 	case cpu_to_le16(IEEE80211_FCTL_TODS):
338 		if (unlikely(iftype != NL80211_IFTYPE_AP &&
339 			     iftype != NL80211_IFTYPE_AP_VLAN &&
340 			     iftype != NL80211_IFTYPE_P2P_GO))
341 			return -1;
342 		break;
343 	case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
344 		if (unlikely(iftype != NL80211_IFTYPE_WDS &&
345 			     iftype != NL80211_IFTYPE_MESH_POINT &&
346 			     iftype != NL80211_IFTYPE_AP_VLAN &&
347 			     iftype != NL80211_IFTYPE_STATION))
348 			return -1;
349 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
350 			struct ieee80211s_hdr *meshdr =
351 				(struct ieee80211s_hdr *) (skb->data + hdrlen);
352 			/* make sure meshdr->flags is on the linear part */
353 			if (!pskb_may_pull(skb, hdrlen + 1))
354 				return -1;
355 			if (meshdr->flags & MESH_FLAGS_AE_A4)
356 				return -1;
357 			if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
358 				skb_copy_bits(skb, hdrlen +
359 					offsetof(struct ieee80211s_hdr, eaddr1),
360 				       	dst, ETH_ALEN);
361 				skb_copy_bits(skb, hdrlen +
362 					offsetof(struct ieee80211s_hdr, eaddr2),
363 				        src, ETH_ALEN);
364 			}
365 			hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
366 		}
367 		break;
368 	case cpu_to_le16(IEEE80211_FCTL_FROMDS):
369 		if ((iftype != NL80211_IFTYPE_STATION &&
370 		     iftype != NL80211_IFTYPE_P2P_CLIENT &&
371 		     iftype != NL80211_IFTYPE_MESH_POINT) ||
372 		    (is_multicast_ether_addr(dst) &&
373 		     !compare_ether_addr(src, addr)))
374 			return -1;
375 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
376 			struct ieee80211s_hdr *meshdr =
377 				(struct ieee80211s_hdr *) (skb->data + hdrlen);
378 			/* make sure meshdr->flags is on the linear part */
379 			if (!pskb_may_pull(skb, hdrlen + 1))
380 				return -1;
381 			if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
382 				return -1;
383 			if (meshdr->flags & MESH_FLAGS_AE_A4)
384 				skb_copy_bits(skb, hdrlen +
385 					offsetof(struct ieee80211s_hdr, eaddr1),
386 					src, ETH_ALEN);
387 			hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
388 		}
389 		break;
390 	case cpu_to_le16(0):
391 		if (iftype != NL80211_IFTYPE_ADHOC &&
392 		    iftype != NL80211_IFTYPE_STATION)
393 				return -1;
394 		break;
395 	}
396 
397 	if (!pskb_may_pull(skb, hdrlen + 8))
398 		return -1;
399 
400 	payload = skb->data + hdrlen;
401 	ethertype = (payload[6] << 8) | payload[7];
402 
403 	if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
404 		    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
405 		   compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
406 		/* remove RFC1042 or Bridge-Tunnel encapsulation and
407 		 * replace EtherType */
408 		skb_pull(skb, hdrlen + 6);
409 		memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
410 		memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
411 	} else {
412 		struct ethhdr *ehdr;
413 		__be16 len;
414 
415 		skb_pull(skb, hdrlen);
416 		len = htons(skb->len);
417 		ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
418 		memcpy(ehdr->h_dest, dst, ETH_ALEN);
419 		memcpy(ehdr->h_source, src, ETH_ALEN);
420 		ehdr->h_proto = len;
421 	}
422 	return 0;
423 }
424 EXPORT_SYMBOL(ieee80211_data_to_8023);
425 
ieee80211_data_from_8023(struct sk_buff * skb,const u8 * addr,enum nl80211_iftype iftype,u8 * bssid,bool qos)426 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
427 			     enum nl80211_iftype iftype, u8 *bssid, bool qos)
428 {
429 	struct ieee80211_hdr hdr;
430 	u16 hdrlen, ethertype;
431 	__le16 fc;
432 	const u8 *encaps_data;
433 	int encaps_len, skip_header_bytes;
434 	int nh_pos, h_pos;
435 	int head_need;
436 
437 	if (unlikely(skb->len < ETH_HLEN))
438 		return -EINVAL;
439 
440 	nh_pos = skb_network_header(skb) - skb->data;
441 	h_pos = skb_transport_header(skb) - skb->data;
442 
443 	/* convert Ethernet header to proper 802.11 header (based on
444 	 * operation mode) */
445 	ethertype = (skb->data[12] << 8) | skb->data[13];
446 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
447 
448 	switch (iftype) {
449 	case NL80211_IFTYPE_AP:
450 	case NL80211_IFTYPE_AP_VLAN:
451 	case NL80211_IFTYPE_P2P_GO:
452 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
453 		/* DA BSSID SA */
454 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
455 		memcpy(hdr.addr2, addr, ETH_ALEN);
456 		memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
457 		hdrlen = 24;
458 		break;
459 	case NL80211_IFTYPE_STATION:
460 	case NL80211_IFTYPE_P2P_CLIENT:
461 		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
462 		/* BSSID SA DA */
463 		memcpy(hdr.addr1, bssid, ETH_ALEN);
464 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
465 		memcpy(hdr.addr3, skb->data, ETH_ALEN);
466 		hdrlen = 24;
467 		break;
468 	case NL80211_IFTYPE_ADHOC:
469 		/* DA SA BSSID */
470 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
471 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
472 		memcpy(hdr.addr3, bssid, ETH_ALEN);
473 		hdrlen = 24;
474 		break;
475 	default:
476 		return -EOPNOTSUPP;
477 	}
478 
479 	if (qos) {
480 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
481 		hdrlen += 2;
482 	}
483 
484 	hdr.frame_control = fc;
485 	hdr.duration_id = 0;
486 	hdr.seq_ctrl = 0;
487 
488 	skip_header_bytes = ETH_HLEN;
489 	if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
490 		encaps_data = bridge_tunnel_header;
491 		encaps_len = sizeof(bridge_tunnel_header);
492 		skip_header_bytes -= 2;
493 	} else if (ethertype > 0x600) {
494 		encaps_data = rfc1042_header;
495 		encaps_len = sizeof(rfc1042_header);
496 		skip_header_bytes -= 2;
497 	} else {
498 		encaps_data = NULL;
499 		encaps_len = 0;
500 	}
501 
502 	skb_pull(skb, skip_header_bytes);
503 	nh_pos -= skip_header_bytes;
504 	h_pos -= skip_header_bytes;
505 
506 	head_need = hdrlen + encaps_len - skb_headroom(skb);
507 
508 	if (head_need > 0 || skb_cloned(skb)) {
509 		head_need = max(head_need, 0);
510 		if (head_need)
511 			skb_orphan(skb);
512 
513 		if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
514 			return -ENOMEM;
515 
516 		skb->truesize += head_need;
517 	}
518 
519 	if (encaps_data) {
520 		memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
521 		nh_pos += encaps_len;
522 		h_pos += encaps_len;
523 	}
524 
525 	memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
526 
527 	nh_pos += hdrlen;
528 	h_pos += hdrlen;
529 
530 	/* Update skb pointers to various headers since this modified frame
531 	 * is going to go through Linux networking code that may potentially
532 	 * need things like pointer to IP header. */
533 	skb_set_mac_header(skb, 0);
534 	skb_set_network_header(skb, nh_pos);
535 	skb_set_transport_header(skb, h_pos);
536 
537 	return 0;
538 }
539 EXPORT_SYMBOL(ieee80211_data_from_8023);
540 
541 
ieee80211_amsdu_to_8023s(struct sk_buff * skb,struct sk_buff_head * list,const u8 * addr,enum nl80211_iftype iftype,const unsigned int extra_headroom,bool has_80211_header)542 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
543 			      const u8 *addr, enum nl80211_iftype iftype,
544 			      const unsigned int extra_headroom,
545 			      bool has_80211_header)
546 {
547 	struct sk_buff *frame = NULL;
548 	u16 ethertype;
549 	u8 *payload;
550 	const struct ethhdr *eth;
551 	int remaining, err;
552 	u8 dst[ETH_ALEN], src[ETH_ALEN];
553 
554 	if (has_80211_header) {
555 		err = ieee80211_data_to_8023(skb, addr, iftype);
556 		if (err)
557 			goto out;
558 
559 		/* skip the wrapping header */
560 		eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
561 		if (!eth)
562 			goto out;
563 	} else {
564 		eth = (struct ethhdr *) skb->data;
565 	}
566 
567 	while (skb != frame) {
568 		u8 padding;
569 		__be16 len = eth->h_proto;
570 		unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
571 
572 		remaining = skb->len;
573 		memcpy(dst, eth->h_dest, ETH_ALEN);
574 		memcpy(src, eth->h_source, ETH_ALEN);
575 
576 		padding = (4 - subframe_len) & 0x3;
577 		/* the last MSDU has no padding */
578 		if (subframe_len > remaining)
579 			goto purge;
580 
581 		skb_pull(skb, sizeof(struct ethhdr));
582 		/* reuse skb for the last subframe */
583 		if (remaining <= subframe_len + padding)
584 			frame = skb;
585 		else {
586 			unsigned int hlen = ALIGN(extra_headroom, 4);
587 			/*
588 			 * Allocate and reserve two bytes more for payload
589 			 * alignment since sizeof(struct ethhdr) is 14.
590 			 */
591 			frame = dev_alloc_skb(hlen + subframe_len + 2);
592 			if (!frame)
593 				goto purge;
594 
595 			skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
596 			memcpy(skb_put(frame, ntohs(len)), skb->data,
597 				ntohs(len));
598 
599 			eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
600 							padding);
601 			if (!eth) {
602 				dev_kfree_skb(frame);
603 				goto purge;
604 			}
605 		}
606 
607 		skb_reset_network_header(frame);
608 		frame->dev = skb->dev;
609 		frame->priority = skb->priority;
610 
611 		payload = frame->data;
612 		ethertype = (payload[6] << 8) | payload[7];
613 
614 		if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
615 			    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
616 			   compare_ether_addr(payload,
617 					      bridge_tunnel_header) == 0)) {
618 			/* remove RFC1042 or Bridge-Tunnel
619 			 * encapsulation and replace EtherType */
620 			skb_pull(frame, 6);
621 			memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
622 			memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
623 		} else {
624 			memcpy(skb_push(frame, sizeof(__be16)), &len,
625 				sizeof(__be16));
626 			memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
627 			memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
628 		}
629 		__skb_queue_tail(list, frame);
630 	}
631 
632 	return;
633 
634  purge:
635 	__skb_queue_purge(list);
636  out:
637 	dev_kfree_skb(skb);
638 }
639 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
640 
641 /* Given a data frame determine the 802.1p/1d tag to use. */
cfg80211_classify8021d(struct sk_buff * skb)642 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
643 {
644 	unsigned int dscp;
645 
646 	/* skb->priority values from 256->263 are magic values to
647 	 * directly indicate a specific 802.1d priority.  This is used
648 	 * to allow 802.1d priority to be passed directly in from VLAN
649 	 * tags, etc.
650 	 */
651 	if (skb->priority >= 256 && skb->priority <= 263)
652 		return skb->priority - 256;
653 
654 	switch (skb->protocol) {
655 	case htons(ETH_P_IP):
656 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
657 		break;
658 	case htons(ETH_P_IPV6):
659 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
660 		break;
661 	default:
662 		return 0;
663 	}
664 
665 	return dscp >> 5;
666 }
667 EXPORT_SYMBOL(cfg80211_classify8021d);
668 
ieee80211_bss_get_ie(struct cfg80211_bss * bss,u8 ie)669 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
670 {
671 	u8 *end, *pos;
672 
673 	pos = bss->information_elements;
674 	if (pos == NULL)
675 		return NULL;
676 	end = pos + bss->len_information_elements;
677 
678 	while (pos + 1 < end) {
679 		if (pos + 2 + pos[1] > end)
680 			break;
681 		if (pos[0] == ie)
682 			return pos;
683 		pos += 2 + pos[1];
684 	}
685 
686 	return NULL;
687 }
688 EXPORT_SYMBOL(ieee80211_bss_get_ie);
689 
cfg80211_upload_connect_keys(struct wireless_dev * wdev)690 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
691 {
692 	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
693 	struct net_device *dev = wdev->netdev;
694 	int i;
695 
696 	if (!wdev->connect_keys)
697 		return;
698 
699 	for (i = 0; i < 6; i++) {
700 		if (!wdev->connect_keys->params[i].cipher)
701 			continue;
702 		if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
703 					&wdev->connect_keys->params[i])) {
704 			netdev_err(dev, "failed to set key %d\n", i);
705 			continue;
706 		}
707 		if (wdev->connect_keys->def == i)
708 			if (rdev->ops->set_default_key(wdev->wiphy, dev,
709 						       i, true, true)) {
710 				netdev_err(dev, "failed to set defkey %d\n", i);
711 				continue;
712 			}
713 		if (wdev->connect_keys->defmgmt == i)
714 			if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
715 				netdev_err(dev, "failed to set mgtdef %d\n", i);
716 	}
717 
718 	kfree(wdev->connect_keys);
719 	wdev->connect_keys = NULL;
720 }
721 
cfg80211_process_wdev_events(struct wireless_dev * wdev)722 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
723 {
724 	struct cfg80211_event *ev;
725 	unsigned long flags;
726 	const u8 *bssid = NULL;
727 
728 	spin_lock_irqsave(&wdev->event_lock, flags);
729 	while (!list_empty(&wdev->event_list)) {
730 		ev = list_first_entry(&wdev->event_list,
731 				      struct cfg80211_event, list);
732 		list_del(&ev->list);
733 		spin_unlock_irqrestore(&wdev->event_lock, flags);
734 
735 		wdev_lock(wdev);
736 		switch (ev->type) {
737 		case EVENT_CONNECT_RESULT:
738 			if (!is_zero_ether_addr(ev->cr.bssid))
739 				bssid = ev->cr.bssid;
740 			__cfg80211_connect_result(
741 				wdev->netdev, bssid,
742 				ev->cr.req_ie, ev->cr.req_ie_len,
743 				ev->cr.resp_ie, ev->cr.resp_ie_len,
744 				ev->cr.status,
745 				ev->cr.status == WLAN_STATUS_SUCCESS,
746 				NULL);
747 			break;
748 		case EVENT_ROAMED:
749 			__cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
750 					  ev->rm.req_ie_len, ev->rm.resp_ie,
751 					  ev->rm.resp_ie_len);
752 			break;
753 		case EVENT_DISCONNECTED:
754 			__cfg80211_disconnected(wdev->netdev,
755 						ev->dc.ie, ev->dc.ie_len,
756 						ev->dc.reason, true);
757 			break;
758 		case EVENT_IBSS_JOINED:
759 			__cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
760 			break;
761 		}
762 		wdev_unlock(wdev);
763 
764 		kfree(ev);
765 
766 		spin_lock_irqsave(&wdev->event_lock, flags);
767 	}
768 	spin_unlock_irqrestore(&wdev->event_lock, flags);
769 }
770 
cfg80211_process_rdev_events(struct cfg80211_registered_device * rdev)771 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
772 {
773 	struct wireless_dev *wdev;
774 
775 	ASSERT_RTNL();
776 	ASSERT_RDEV_LOCK(rdev);
777 
778 	mutex_lock(&rdev->devlist_mtx);
779 
780 	list_for_each_entry(wdev, &rdev->netdev_list, list)
781 		cfg80211_process_wdev_events(wdev);
782 
783 	mutex_unlock(&rdev->devlist_mtx);
784 }
785 
cfg80211_change_iface(struct cfg80211_registered_device * rdev,struct net_device * dev,enum nl80211_iftype ntype,u32 * flags,struct vif_params * params)786 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
787 			  struct net_device *dev, enum nl80211_iftype ntype,
788 			  u32 *flags, struct vif_params *params)
789 {
790 	int err;
791 	enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
792 
793 	ASSERT_RDEV_LOCK(rdev);
794 
795 	/* don't support changing VLANs, you just re-create them */
796 	if (otype == NL80211_IFTYPE_AP_VLAN)
797 		return -EOPNOTSUPP;
798 
799 	if (!rdev->ops->change_virtual_intf ||
800 	    !(rdev->wiphy.interface_modes & (1 << ntype)))
801 		return -EOPNOTSUPP;
802 
803 	/* if it's part of a bridge, reject changing type to station/ibss */
804 	if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
805 	    (ntype == NL80211_IFTYPE_ADHOC ||
806 	     ntype == NL80211_IFTYPE_STATION ||
807 	     ntype == NL80211_IFTYPE_P2P_CLIENT))
808 		return -EBUSY;
809 
810 	if (ntype != otype && netif_running(dev)) {
811 		err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
812 						    ntype);
813 		if (err)
814 			return err;
815 
816 		dev->ieee80211_ptr->use_4addr = false;
817 		dev->ieee80211_ptr->mesh_id_up_len = 0;
818 
819 		switch (otype) {
820 		case NL80211_IFTYPE_ADHOC:
821 			cfg80211_leave_ibss(rdev, dev, false);
822 			break;
823 		case NL80211_IFTYPE_STATION:
824 		case NL80211_IFTYPE_P2P_CLIENT:
825 			cfg80211_disconnect(rdev, dev,
826 					    WLAN_REASON_DEAUTH_LEAVING, true);
827 			break;
828 		case NL80211_IFTYPE_MESH_POINT:
829 			/* mesh should be handled? */
830 			break;
831 		default:
832 			break;
833 		}
834 
835 		cfg80211_process_rdev_events(rdev);
836 	}
837 
838 	err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
839 					     ntype, flags, params);
840 
841 	WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
842 
843 	if (!err && params && params->use_4addr != -1)
844 		dev->ieee80211_ptr->use_4addr = params->use_4addr;
845 
846 	if (!err) {
847 		dev->priv_flags &= ~IFF_DONT_BRIDGE;
848 		switch (ntype) {
849 		case NL80211_IFTYPE_STATION:
850 			if (dev->ieee80211_ptr->use_4addr)
851 				break;
852 			/* fall through */
853 		case NL80211_IFTYPE_P2P_CLIENT:
854 		case NL80211_IFTYPE_ADHOC:
855 			dev->priv_flags |= IFF_DONT_BRIDGE;
856 			break;
857 		case NL80211_IFTYPE_P2P_GO:
858 		case NL80211_IFTYPE_AP:
859 		case NL80211_IFTYPE_AP_VLAN:
860 		case NL80211_IFTYPE_WDS:
861 		case NL80211_IFTYPE_MESH_POINT:
862 			/* bridging OK */
863 			break;
864 		case NL80211_IFTYPE_MONITOR:
865 			/* monitor can't bridge anyway */
866 			break;
867 		case NL80211_IFTYPE_UNSPECIFIED:
868 		case NUM_NL80211_IFTYPES:
869 			/* not happening */
870 			break;
871 		}
872 	}
873 
874 	return err;
875 }
876 
cfg80211_calculate_bitrate(struct rate_info * rate)877 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
878 {
879 	int modulation, streams, bitrate;
880 
881 	if (!(rate->flags & RATE_INFO_FLAGS_MCS))
882 		return rate->legacy;
883 
884 	/* the formula below does only work for MCS values smaller than 32 */
885 	if (rate->mcs >= 32)
886 		return 0;
887 
888 	modulation = rate->mcs & 7;
889 	streams = (rate->mcs >> 3) + 1;
890 
891 	bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
892 			13500000 : 6500000;
893 
894 	if (modulation < 4)
895 		bitrate *= (modulation + 1);
896 	else if (modulation == 4)
897 		bitrate *= (modulation + 2);
898 	else
899 		bitrate *= (modulation + 3);
900 
901 	bitrate *= streams;
902 
903 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
904 		bitrate = (bitrate / 9) * 10;
905 
906 	/* do NOT round down here */
907 	return (bitrate + 50000) / 100000;
908 }
909 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
910 
cfg80211_validate_beacon_int(struct cfg80211_registered_device * rdev,u32 beacon_int)911 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
912 				 u32 beacon_int)
913 {
914 	struct wireless_dev *wdev;
915 	int res = 0;
916 
917 	if (!beacon_int)
918 		return -EINVAL;
919 
920 	mutex_lock(&rdev->devlist_mtx);
921 
922 	list_for_each_entry(wdev, &rdev->netdev_list, list) {
923 		if (!wdev->beacon_interval)
924 			continue;
925 		if (wdev->beacon_interval != beacon_int) {
926 			res = -EINVAL;
927 			break;
928 		}
929 	}
930 
931 	mutex_unlock(&rdev->devlist_mtx);
932 
933 	return res;
934 }
935 
cfg80211_can_change_interface(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,enum nl80211_iftype iftype)936 int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
937 				  struct wireless_dev *wdev,
938 				  enum nl80211_iftype iftype)
939 {
940 	struct wireless_dev *wdev_iter;
941 	u32 used_iftypes = BIT(iftype);
942 	int num[NUM_NL80211_IFTYPES];
943 	int total = 1;
944 	int i, j;
945 
946 	ASSERT_RTNL();
947 
948 	/* Always allow software iftypes */
949 	if (rdev->wiphy.software_iftypes & BIT(iftype))
950 		return 0;
951 
952 	/*
953 	 * Drivers will gradually all set this flag, until all
954 	 * have it we only enforce for those that set it.
955 	 */
956 	if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
957 		return 0;
958 
959 	memset(num, 0, sizeof(num));
960 
961 	num[iftype] = 1;
962 
963 	mutex_lock(&rdev->devlist_mtx);
964 	list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
965 		if (wdev_iter == wdev)
966 			continue;
967 		if (!netif_running(wdev_iter->netdev))
968 			continue;
969 
970 		if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
971 			continue;
972 
973 		num[wdev_iter->iftype]++;
974 		total++;
975 		used_iftypes |= BIT(wdev_iter->iftype);
976 	}
977 	mutex_unlock(&rdev->devlist_mtx);
978 
979 	if (total == 1)
980 		return 0;
981 
982 	for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
983 		const struct ieee80211_iface_combination *c;
984 		struct ieee80211_iface_limit *limits;
985 		u32 all_iftypes = 0;
986 
987 		c = &rdev->wiphy.iface_combinations[i];
988 
989 		limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
990 				 GFP_KERNEL);
991 		if (!limits)
992 			return -ENOMEM;
993 		if (total > c->max_interfaces)
994 			goto cont;
995 
996 		for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
997 			if (rdev->wiphy.software_iftypes & BIT(iftype))
998 				continue;
999 			for (j = 0; j < c->n_limits; j++) {
1000 				all_iftypes |= limits[j].types;
1001 				if (!(limits[j].types & BIT(iftype)))
1002 					continue;
1003 				if (limits[j].max < num[iftype])
1004 					goto cont;
1005 				limits[j].max -= num[iftype];
1006 			}
1007 		}
1008 
1009 		/*
1010 		 * Finally check that all iftypes that we're currently
1011 		 * using are actually part of this combination. If they
1012 		 * aren't then we can't use this combination and have
1013 		 * to continue to the next.
1014 		 */
1015 		if ((all_iftypes & used_iftypes) != used_iftypes)
1016 			goto cont;
1017 
1018 		/*
1019 		 * This combination covered all interface types and
1020 		 * supported the requested numbers, so we're good.
1021 		 */
1022 		kfree(limits);
1023 		return 0;
1024  cont:
1025 		kfree(limits);
1026 	}
1027 
1028 	return -EBUSY;
1029 }
1030 
ieee80211_get_ratemask(struct ieee80211_supported_band * sband,const u8 * rates,unsigned int n_rates,u32 * mask)1031 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1032 			   const u8 *rates, unsigned int n_rates,
1033 			   u32 *mask)
1034 {
1035 	int i, j;
1036 
1037 	if (!sband)
1038 		return -EINVAL;
1039 
1040 	if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1041 		return -EINVAL;
1042 
1043 	*mask = 0;
1044 
1045 	for (i = 0; i < n_rates; i++) {
1046 		int rate = (rates[i] & 0x7f) * 5;
1047 		bool found = false;
1048 
1049 		for (j = 0; j < sband->n_bitrates; j++) {
1050 			if (sband->bitrates[j].bitrate == rate) {
1051 				found = true;
1052 				*mask |= BIT(j);
1053 				break;
1054 			}
1055 		}
1056 		if (!found)
1057 			return -EINVAL;
1058 	}
1059 
1060 	/*
1061 	 * mask must have at least one bit set here since we
1062 	 * didn't accept a 0-length rates array nor allowed
1063 	 * entries in the array that didn't exist
1064 	 */
1065 
1066 	return 0;
1067 }
1068 
1069 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1070 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1071 const unsigned char rfc1042_header[] __aligned(2) =
1072 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1073 EXPORT_SYMBOL(rfc1042_header);
1074 
1075 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1076 const unsigned char bridge_tunnel_header[] __aligned(2) =
1077 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1078 EXPORT_SYMBOL(bridge_tunnel_header);
1079