xref: /linux/net/wireless/util.c (revision 2dbc0838bcf24ca59cabc3130cf3b1d6809cdcd4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Wireless utility functions
4  *
5  * Copyright 2007-2009	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2017	Intel Deutschland GmbH
8  * Copyright (C) 2018-2019 Intel Corporation
9  */
10 #include <linux/export.h>
11 #include <linux/bitops.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ieee80211.h>
15 #include <net/cfg80211.h>
16 #include <net/ip.h>
17 #include <net/dsfield.h>
18 #include <linux/if_vlan.h>
19 #include <linux/mpls.h>
20 #include <linux/gcd.h>
21 #include <linux/bitfield.h>
22 #include <linux/nospec.h>
23 #include "core.h"
24 #include "rdev-ops.h"
25 
26 
27 struct ieee80211_rate *
28 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
29 			    u32 basic_rates, int bitrate)
30 {
31 	struct ieee80211_rate *result = &sband->bitrates[0];
32 	int i;
33 
34 	for (i = 0; i < sband->n_bitrates; i++) {
35 		if (!(basic_rates & BIT(i)))
36 			continue;
37 		if (sband->bitrates[i].bitrate > bitrate)
38 			continue;
39 		result = &sband->bitrates[i];
40 	}
41 
42 	return result;
43 }
44 EXPORT_SYMBOL(ieee80211_get_response_rate);
45 
46 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
47 			      enum nl80211_bss_scan_width scan_width)
48 {
49 	struct ieee80211_rate *bitrates;
50 	u32 mandatory_rates = 0;
51 	enum ieee80211_rate_flags mandatory_flag;
52 	int i;
53 
54 	if (WARN_ON(!sband))
55 		return 1;
56 
57 	if (sband->band == NL80211_BAND_2GHZ) {
58 		if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
59 		    scan_width == NL80211_BSS_CHAN_WIDTH_10)
60 			mandatory_flag = IEEE80211_RATE_MANDATORY_G;
61 		else
62 			mandatory_flag = IEEE80211_RATE_MANDATORY_B;
63 	} else {
64 		mandatory_flag = IEEE80211_RATE_MANDATORY_A;
65 	}
66 
67 	bitrates = sband->bitrates;
68 	for (i = 0; i < sband->n_bitrates; i++)
69 		if (bitrates[i].flags & mandatory_flag)
70 			mandatory_rates |= BIT(i);
71 	return mandatory_rates;
72 }
73 EXPORT_SYMBOL(ieee80211_mandatory_rates);
74 
75 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
76 {
77 	/* see 802.11 17.3.8.3.2 and Annex J
78 	 * there are overlapping channel numbers in 5GHz and 2GHz bands */
79 	if (chan <= 0)
80 		return 0; /* not supported */
81 	switch (band) {
82 	case NL80211_BAND_2GHZ:
83 		if (chan == 14)
84 			return 2484;
85 		else if (chan < 14)
86 			return 2407 + chan * 5;
87 		break;
88 	case NL80211_BAND_5GHZ:
89 		if (chan >= 182 && chan <= 196)
90 			return 4000 + chan * 5;
91 		else
92 			return 5000 + chan * 5;
93 		break;
94 	case NL80211_BAND_60GHZ:
95 		if (chan < 7)
96 			return 56160 + chan * 2160;
97 		break;
98 	default:
99 		;
100 	}
101 	return 0; /* not supported */
102 }
103 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
104 
105 int ieee80211_frequency_to_channel(int freq)
106 {
107 	/* see 802.11 17.3.8.3.2 and Annex J */
108 	if (freq == 2484)
109 		return 14;
110 	else if (freq < 2484)
111 		return (freq - 2407) / 5;
112 	else if (freq >= 4910 && freq <= 4980)
113 		return (freq - 4000) / 5;
114 	else if (freq <= 45000) /* DMG band lower limit */
115 		return (freq - 5000) / 5;
116 	else if (freq >= 58320 && freq <= 70200)
117 		return (freq - 56160) / 2160;
118 	else
119 		return 0;
120 }
121 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
122 
123 struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
124 {
125 	enum nl80211_band band;
126 	struct ieee80211_supported_band *sband;
127 	int i;
128 
129 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
130 		sband = wiphy->bands[band];
131 
132 		if (!sband)
133 			continue;
134 
135 		for (i = 0; i < sband->n_channels; i++) {
136 			if (sband->channels[i].center_freq == freq)
137 				return &sband->channels[i];
138 		}
139 	}
140 
141 	return NULL;
142 }
143 EXPORT_SYMBOL(ieee80211_get_channel);
144 
145 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
146 {
147 	int i, want;
148 
149 	switch (sband->band) {
150 	case NL80211_BAND_5GHZ:
151 		want = 3;
152 		for (i = 0; i < sband->n_bitrates; i++) {
153 			if (sband->bitrates[i].bitrate == 60 ||
154 			    sband->bitrates[i].bitrate == 120 ||
155 			    sband->bitrates[i].bitrate == 240) {
156 				sband->bitrates[i].flags |=
157 					IEEE80211_RATE_MANDATORY_A;
158 				want--;
159 			}
160 		}
161 		WARN_ON(want);
162 		break;
163 	case NL80211_BAND_2GHZ:
164 		want = 7;
165 		for (i = 0; i < sband->n_bitrates; i++) {
166 			switch (sband->bitrates[i].bitrate) {
167 			case 10:
168 			case 20:
169 			case 55:
170 			case 110:
171 				sband->bitrates[i].flags |=
172 					IEEE80211_RATE_MANDATORY_B |
173 					IEEE80211_RATE_MANDATORY_G;
174 				want--;
175 				break;
176 			case 60:
177 			case 120:
178 			case 240:
179 				sband->bitrates[i].flags |=
180 					IEEE80211_RATE_MANDATORY_G;
181 				want--;
182 				/* fall through */
183 			default:
184 				sband->bitrates[i].flags |=
185 					IEEE80211_RATE_ERP_G;
186 				break;
187 			}
188 		}
189 		WARN_ON(want != 0 && want != 3);
190 		break;
191 	case NL80211_BAND_60GHZ:
192 		/* check for mandatory HT MCS 1..4 */
193 		WARN_ON(!sband->ht_cap.ht_supported);
194 		WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
195 		break;
196 	case NUM_NL80211_BANDS:
197 	default:
198 		WARN_ON(1);
199 		break;
200 	}
201 }
202 
203 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
204 {
205 	enum nl80211_band band;
206 
207 	for (band = 0; band < NUM_NL80211_BANDS; band++)
208 		if (wiphy->bands[band])
209 			set_mandatory_flags_band(wiphy->bands[band]);
210 }
211 
212 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
213 {
214 	int i;
215 	for (i = 0; i < wiphy->n_cipher_suites; i++)
216 		if (cipher == wiphy->cipher_suites[i])
217 			return true;
218 	return false;
219 }
220 
221 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
222 				   struct key_params *params, int key_idx,
223 				   bool pairwise, const u8 *mac_addr)
224 {
225 	if (key_idx < 0 || key_idx > 5)
226 		return -EINVAL;
227 
228 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
229 		return -EINVAL;
230 
231 	if (pairwise && !mac_addr)
232 		return -EINVAL;
233 
234 	switch (params->cipher) {
235 	case WLAN_CIPHER_SUITE_TKIP:
236 	case WLAN_CIPHER_SUITE_CCMP:
237 	case WLAN_CIPHER_SUITE_CCMP_256:
238 	case WLAN_CIPHER_SUITE_GCMP:
239 	case WLAN_CIPHER_SUITE_GCMP_256:
240 		/* IEEE802.11-2016 allows only 0 and - when using Extended Key
241 		 * ID - 1 as index for pairwise keys.
242 		 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
243 		 * the driver supports Extended Key ID.
244 		 * @NL80211_KEY_SET_TX can't be set when installing and
245 		 * validating a key.
246 		 */
247 		if (params->mode == NL80211_KEY_NO_TX) {
248 			if (!wiphy_ext_feature_isset(&rdev->wiphy,
249 						     NL80211_EXT_FEATURE_EXT_KEY_ID))
250 				return -EINVAL;
251 			else if (!pairwise || key_idx < 0 || key_idx > 1)
252 				return -EINVAL;
253 		} else if ((pairwise && key_idx) ||
254 			   params->mode == NL80211_KEY_SET_TX) {
255 			return -EINVAL;
256 		}
257 		break;
258 	case WLAN_CIPHER_SUITE_AES_CMAC:
259 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
260 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
261 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
262 		/* Disallow BIP (group-only) cipher as pairwise cipher */
263 		if (pairwise)
264 			return -EINVAL;
265 		if (key_idx < 4)
266 			return -EINVAL;
267 		break;
268 	case WLAN_CIPHER_SUITE_WEP40:
269 	case WLAN_CIPHER_SUITE_WEP104:
270 		if (key_idx > 3)
271 			return -EINVAL;
272 	default:
273 		break;
274 	}
275 
276 	switch (params->cipher) {
277 	case WLAN_CIPHER_SUITE_WEP40:
278 		if (params->key_len != WLAN_KEY_LEN_WEP40)
279 			return -EINVAL;
280 		break;
281 	case WLAN_CIPHER_SUITE_TKIP:
282 		if (params->key_len != WLAN_KEY_LEN_TKIP)
283 			return -EINVAL;
284 		break;
285 	case WLAN_CIPHER_SUITE_CCMP:
286 		if (params->key_len != WLAN_KEY_LEN_CCMP)
287 			return -EINVAL;
288 		break;
289 	case WLAN_CIPHER_SUITE_CCMP_256:
290 		if (params->key_len != WLAN_KEY_LEN_CCMP_256)
291 			return -EINVAL;
292 		break;
293 	case WLAN_CIPHER_SUITE_GCMP:
294 		if (params->key_len != WLAN_KEY_LEN_GCMP)
295 			return -EINVAL;
296 		break;
297 	case WLAN_CIPHER_SUITE_GCMP_256:
298 		if (params->key_len != WLAN_KEY_LEN_GCMP_256)
299 			return -EINVAL;
300 		break;
301 	case WLAN_CIPHER_SUITE_WEP104:
302 		if (params->key_len != WLAN_KEY_LEN_WEP104)
303 			return -EINVAL;
304 		break;
305 	case WLAN_CIPHER_SUITE_AES_CMAC:
306 		if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
307 			return -EINVAL;
308 		break;
309 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
310 		if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
311 			return -EINVAL;
312 		break;
313 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
314 		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
315 			return -EINVAL;
316 		break;
317 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
318 		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
319 			return -EINVAL;
320 		break;
321 	default:
322 		/*
323 		 * We don't know anything about this algorithm,
324 		 * allow using it -- but the driver must check
325 		 * all parameters! We still check below whether
326 		 * or not the driver supports this algorithm,
327 		 * of course.
328 		 */
329 		break;
330 	}
331 
332 	if (params->seq) {
333 		switch (params->cipher) {
334 		case WLAN_CIPHER_SUITE_WEP40:
335 		case WLAN_CIPHER_SUITE_WEP104:
336 			/* These ciphers do not use key sequence */
337 			return -EINVAL;
338 		case WLAN_CIPHER_SUITE_TKIP:
339 		case WLAN_CIPHER_SUITE_CCMP:
340 		case WLAN_CIPHER_SUITE_CCMP_256:
341 		case WLAN_CIPHER_SUITE_GCMP:
342 		case WLAN_CIPHER_SUITE_GCMP_256:
343 		case WLAN_CIPHER_SUITE_AES_CMAC:
344 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
345 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
346 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
347 			if (params->seq_len != 6)
348 				return -EINVAL;
349 			break;
350 		}
351 	}
352 
353 	if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
354 		return -EINVAL;
355 
356 	return 0;
357 }
358 
359 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
360 {
361 	unsigned int hdrlen = 24;
362 
363 	if (ieee80211_is_data(fc)) {
364 		if (ieee80211_has_a4(fc))
365 			hdrlen = 30;
366 		if (ieee80211_is_data_qos(fc)) {
367 			hdrlen += IEEE80211_QOS_CTL_LEN;
368 			if (ieee80211_has_order(fc))
369 				hdrlen += IEEE80211_HT_CTL_LEN;
370 		}
371 		goto out;
372 	}
373 
374 	if (ieee80211_is_mgmt(fc)) {
375 		if (ieee80211_has_order(fc))
376 			hdrlen += IEEE80211_HT_CTL_LEN;
377 		goto out;
378 	}
379 
380 	if (ieee80211_is_ctl(fc)) {
381 		/*
382 		 * ACK and CTS are 10 bytes, all others 16. To see how
383 		 * to get this condition consider
384 		 *   subtype mask:   0b0000000011110000 (0x00F0)
385 		 *   ACK subtype:    0b0000000011010000 (0x00D0)
386 		 *   CTS subtype:    0b0000000011000000 (0x00C0)
387 		 *   bits that matter:         ^^^      (0x00E0)
388 		 *   value of those: 0b0000000011000000 (0x00C0)
389 		 */
390 		if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
391 			hdrlen = 10;
392 		else
393 			hdrlen = 16;
394 	}
395 out:
396 	return hdrlen;
397 }
398 EXPORT_SYMBOL(ieee80211_hdrlen);
399 
400 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
401 {
402 	const struct ieee80211_hdr *hdr =
403 			(const struct ieee80211_hdr *)skb->data;
404 	unsigned int hdrlen;
405 
406 	if (unlikely(skb->len < 10))
407 		return 0;
408 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
409 	if (unlikely(hdrlen > skb->len))
410 		return 0;
411 	return hdrlen;
412 }
413 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
414 
415 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
416 {
417 	int ae = flags & MESH_FLAGS_AE;
418 	/* 802.11-2012, 8.2.4.7.3 */
419 	switch (ae) {
420 	default:
421 	case 0:
422 		return 6;
423 	case MESH_FLAGS_AE_A4:
424 		return 12;
425 	case MESH_FLAGS_AE_A5_A6:
426 		return 18;
427 	}
428 }
429 
430 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
431 {
432 	return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
433 }
434 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
435 
436 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
437 				  const u8 *addr, enum nl80211_iftype iftype,
438 				  u8 data_offset)
439 {
440 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
441 	struct {
442 		u8 hdr[ETH_ALEN] __aligned(2);
443 		__be16 proto;
444 	} payload;
445 	struct ethhdr tmp;
446 	u16 hdrlen;
447 	u8 mesh_flags = 0;
448 
449 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
450 		return -1;
451 
452 	hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
453 	if (skb->len < hdrlen + 8)
454 		return -1;
455 
456 	/* convert IEEE 802.11 header + possible LLC headers into Ethernet
457 	 * header
458 	 * IEEE 802.11 address fields:
459 	 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
460 	 *   0     0   DA    SA    BSSID n/a
461 	 *   0     1   DA    BSSID SA    n/a
462 	 *   1     0   BSSID SA    DA    n/a
463 	 *   1     1   RA    TA    DA    SA
464 	 */
465 	memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
466 	memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
467 
468 	if (iftype == NL80211_IFTYPE_MESH_POINT)
469 		skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
470 
471 	mesh_flags &= MESH_FLAGS_AE;
472 
473 	switch (hdr->frame_control &
474 		cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
475 	case cpu_to_le16(IEEE80211_FCTL_TODS):
476 		if (unlikely(iftype != NL80211_IFTYPE_AP &&
477 			     iftype != NL80211_IFTYPE_AP_VLAN &&
478 			     iftype != NL80211_IFTYPE_P2P_GO))
479 			return -1;
480 		break;
481 	case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
482 		if (unlikely(iftype != NL80211_IFTYPE_WDS &&
483 			     iftype != NL80211_IFTYPE_MESH_POINT &&
484 			     iftype != NL80211_IFTYPE_AP_VLAN &&
485 			     iftype != NL80211_IFTYPE_STATION))
486 			return -1;
487 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
488 			if (mesh_flags == MESH_FLAGS_AE_A4)
489 				return -1;
490 			if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
491 				skb_copy_bits(skb, hdrlen +
492 					offsetof(struct ieee80211s_hdr, eaddr1),
493 					tmp.h_dest, 2 * ETH_ALEN);
494 			}
495 			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
496 		}
497 		break;
498 	case cpu_to_le16(IEEE80211_FCTL_FROMDS):
499 		if ((iftype != NL80211_IFTYPE_STATION &&
500 		     iftype != NL80211_IFTYPE_P2P_CLIENT &&
501 		     iftype != NL80211_IFTYPE_MESH_POINT) ||
502 		    (is_multicast_ether_addr(tmp.h_dest) &&
503 		     ether_addr_equal(tmp.h_source, addr)))
504 			return -1;
505 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
506 			if (mesh_flags == MESH_FLAGS_AE_A5_A6)
507 				return -1;
508 			if (mesh_flags == MESH_FLAGS_AE_A4)
509 				skb_copy_bits(skb, hdrlen +
510 					offsetof(struct ieee80211s_hdr, eaddr1),
511 					tmp.h_source, ETH_ALEN);
512 			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
513 		}
514 		break;
515 	case cpu_to_le16(0):
516 		if (iftype != NL80211_IFTYPE_ADHOC &&
517 		    iftype != NL80211_IFTYPE_STATION &&
518 		    iftype != NL80211_IFTYPE_OCB)
519 				return -1;
520 		break;
521 	}
522 
523 	skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
524 	tmp.h_proto = payload.proto;
525 
526 	if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
527 		    tmp.h_proto != htons(ETH_P_AARP) &&
528 		    tmp.h_proto != htons(ETH_P_IPX)) ||
529 		   ether_addr_equal(payload.hdr, bridge_tunnel_header)))
530 		/* remove RFC1042 or Bridge-Tunnel encapsulation and
531 		 * replace EtherType */
532 		hdrlen += ETH_ALEN + 2;
533 	else
534 		tmp.h_proto = htons(skb->len - hdrlen);
535 
536 	pskb_pull(skb, hdrlen);
537 
538 	if (!ehdr)
539 		ehdr = skb_push(skb, sizeof(struct ethhdr));
540 	memcpy(ehdr, &tmp, sizeof(tmp));
541 
542 	return 0;
543 }
544 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
545 
546 static void
547 __frame_add_frag(struct sk_buff *skb, struct page *page,
548 		 void *ptr, int len, int size)
549 {
550 	struct skb_shared_info *sh = skb_shinfo(skb);
551 	int page_offset;
552 
553 	page_ref_inc(page);
554 	page_offset = ptr - page_address(page);
555 	skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
556 }
557 
558 static void
559 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
560 			    int offset, int len)
561 {
562 	struct skb_shared_info *sh = skb_shinfo(skb);
563 	const skb_frag_t *frag = &sh->frags[0];
564 	struct page *frag_page;
565 	void *frag_ptr;
566 	int frag_len, frag_size;
567 	int head_size = skb->len - skb->data_len;
568 	int cur_len;
569 
570 	frag_page = virt_to_head_page(skb->head);
571 	frag_ptr = skb->data;
572 	frag_size = head_size;
573 
574 	while (offset >= frag_size) {
575 		offset -= frag_size;
576 		frag_page = skb_frag_page(frag);
577 		frag_ptr = skb_frag_address(frag);
578 		frag_size = skb_frag_size(frag);
579 		frag++;
580 	}
581 
582 	frag_ptr += offset;
583 	frag_len = frag_size - offset;
584 
585 	cur_len = min(len, frag_len);
586 
587 	__frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
588 	len -= cur_len;
589 
590 	while (len > 0) {
591 		frag_len = skb_frag_size(frag);
592 		cur_len = min(len, frag_len);
593 		__frame_add_frag(frame, skb_frag_page(frag),
594 				 skb_frag_address(frag), cur_len, frag_len);
595 		len -= cur_len;
596 		frag++;
597 	}
598 }
599 
600 static struct sk_buff *
601 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
602 		       int offset, int len, bool reuse_frag)
603 {
604 	struct sk_buff *frame;
605 	int cur_len = len;
606 
607 	if (skb->len - offset < len)
608 		return NULL;
609 
610 	/*
611 	 * When reusing framents, copy some data to the head to simplify
612 	 * ethernet header handling and speed up protocol header processing
613 	 * in the stack later.
614 	 */
615 	if (reuse_frag)
616 		cur_len = min_t(int, len, 32);
617 
618 	/*
619 	 * Allocate and reserve two bytes more for payload
620 	 * alignment since sizeof(struct ethhdr) is 14.
621 	 */
622 	frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
623 	if (!frame)
624 		return NULL;
625 
626 	skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
627 	skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
628 
629 	len -= cur_len;
630 	if (!len)
631 		return frame;
632 
633 	offset += cur_len;
634 	__ieee80211_amsdu_copy_frag(skb, frame, offset, len);
635 
636 	return frame;
637 }
638 
639 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
640 			      const u8 *addr, enum nl80211_iftype iftype,
641 			      const unsigned int extra_headroom,
642 			      const u8 *check_da, const u8 *check_sa)
643 {
644 	unsigned int hlen = ALIGN(extra_headroom, 4);
645 	struct sk_buff *frame = NULL;
646 	u16 ethertype;
647 	u8 *payload;
648 	int offset = 0, remaining;
649 	struct ethhdr eth;
650 	bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
651 	bool reuse_skb = false;
652 	bool last = false;
653 
654 	while (!last) {
655 		unsigned int subframe_len;
656 		int len;
657 		u8 padding;
658 
659 		skb_copy_bits(skb, offset, &eth, sizeof(eth));
660 		len = ntohs(eth.h_proto);
661 		subframe_len = sizeof(struct ethhdr) + len;
662 		padding = (4 - subframe_len) & 0x3;
663 
664 		/* the last MSDU has no padding */
665 		remaining = skb->len - offset;
666 		if (subframe_len > remaining)
667 			goto purge;
668 
669 		offset += sizeof(struct ethhdr);
670 		last = remaining <= subframe_len + padding;
671 
672 		/* FIXME: should we really accept multicast DA? */
673 		if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
674 		     !ether_addr_equal(check_da, eth.h_dest)) ||
675 		    (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
676 			offset += len + padding;
677 			continue;
678 		}
679 
680 		/* reuse skb for the last subframe */
681 		if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
682 			skb_pull(skb, offset);
683 			frame = skb;
684 			reuse_skb = true;
685 		} else {
686 			frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
687 						       reuse_frag);
688 			if (!frame)
689 				goto purge;
690 
691 			offset += len + padding;
692 		}
693 
694 		skb_reset_network_header(frame);
695 		frame->dev = skb->dev;
696 		frame->priority = skb->priority;
697 
698 		payload = frame->data;
699 		ethertype = (payload[6] << 8) | payload[7];
700 		if (likely((ether_addr_equal(payload, rfc1042_header) &&
701 			    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
702 			   ether_addr_equal(payload, bridge_tunnel_header))) {
703 			eth.h_proto = htons(ethertype);
704 			skb_pull(frame, ETH_ALEN + 2);
705 		}
706 
707 		memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
708 		__skb_queue_tail(list, frame);
709 	}
710 
711 	if (!reuse_skb)
712 		dev_kfree_skb(skb);
713 
714 	return;
715 
716  purge:
717 	__skb_queue_purge(list);
718 	dev_kfree_skb(skb);
719 }
720 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
721 
722 /* Given a data frame determine the 802.1p/1d tag to use. */
723 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
724 				    struct cfg80211_qos_map *qos_map)
725 {
726 	unsigned int dscp;
727 	unsigned char vlan_priority;
728 	unsigned int ret;
729 
730 	/* skb->priority values from 256->263 are magic values to
731 	 * directly indicate a specific 802.1d priority.  This is used
732 	 * to allow 802.1d priority to be passed directly in from VLAN
733 	 * tags, etc.
734 	 */
735 	if (skb->priority >= 256 && skb->priority <= 263) {
736 		ret = skb->priority - 256;
737 		goto out;
738 	}
739 
740 	if (skb_vlan_tag_present(skb)) {
741 		vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
742 			>> VLAN_PRIO_SHIFT;
743 		if (vlan_priority > 0) {
744 			ret = vlan_priority;
745 			goto out;
746 		}
747 	}
748 
749 	switch (skb->protocol) {
750 	case htons(ETH_P_IP):
751 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
752 		break;
753 	case htons(ETH_P_IPV6):
754 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
755 		break;
756 	case htons(ETH_P_MPLS_UC):
757 	case htons(ETH_P_MPLS_MC): {
758 		struct mpls_label mpls_tmp, *mpls;
759 
760 		mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
761 					  sizeof(*mpls), &mpls_tmp);
762 		if (!mpls)
763 			return 0;
764 
765 		ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
766 			>> MPLS_LS_TC_SHIFT;
767 		goto out;
768 	}
769 	case htons(ETH_P_80221):
770 		/* 802.21 is always network control traffic */
771 		return 7;
772 	default:
773 		return 0;
774 	}
775 
776 	if (qos_map) {
777 		unsigned int i, tmp_dscp = dscp >> 2;
778 
779 		for (i = 0; i < qos_map->num_des; i++) {
780 			if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
781 				ret = qos_map->dscp_exception[i].up;
782 				goto out;
783 			}
784 		}
785 
786 		for (i = 0; i < 8; i++) {
787 			if (tmp_dscp >= qos_map->up[i].low &&
788 			    tmp_dscp <= qos_map->up[i].high) {
789 				ret = i;
790 				goto out;
791 			}
792 		}
793 	}
794 
795 	ret = dscp >> 5;
796 out:
797 	return array_index_nospec(ret, IEEE80211_NUM_TIDS);
798 }
799 EXPORT_SYMBOL(cfg80211_classify8021d);
800 
801 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
802 {
803 	const struct cfg80211_bss_ies *ies;
804 
805 	ies = rcu_dereference(bss->ies);
806 	if (!ies)
807 		return NULL;
808 
809 	return cfg80211_find_elem(id, ies->data, ies->len);
810 }
811 EXPORT_SYMBOL(ieee80211_bss_get_elem);
812 
813 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
814 {
815 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
816 	struct net_device *dev = wdev->netdev;
817 	int i;
818 
819 	if (!wdev->connect_keys)
820 		return;
821 
822 	for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
823 		if (!wdev->connect_keys->params[i].cipher)
824 			continue;
825 		if (rdev_add_key(rdev, dev, i, false, NULL,
826 				 &wdev->connect_keys->params[i])) {
827 			netdev_err(dev, "failed to set key %d\n", i);
828 			continue;
829 		}
830 		if (wdev->connect_keys->def == i &&
831 		    rdev_set_default_key(rdev, dev, i, true, true)) {
832 			netdev_err(dev, "failed to set defkey %d\n", i);
833 			continue;
834 		}
835 	}
836 
837 	kzfree(wdev->connect_keys);
838 	wdev->connect_keys = NULL;
839 }
840 
841 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
842 {
843 	struct cfg80211_event *ev;
844 	unsigned long flags;
845 
846 	spin_lock_irqsave(&wdev->event_lock, flags);
847 	while (!list_empty(&wdev->event_list)) {
848 		ev = list_first_entry(&wdev->event_list,
849 				      struct cfg80211_event, list);
850 		list_del(&ev->list);
851 		spin_unlock_irqrestore(&wdev->event_lock, flags);
852 
853 		wdev_lock(wdev);
854 		switch (ev->type) {
855 		case EVENT_CONNECT_RESULT:
856 			__cfg80211_connect_result(
857 				wdev->netdev,
858 				&ev->cr,
859 				ev->cr.status == WLAN_STATUS_SUCCESS);
860 			break;
861 		case EVENT_ROAMED:
862 			__cfg80211_roamed(wdev, &ev->rm);
863 			break;
864 		case EVENT_DISCONNECTED:
865 			__cfg80211_disconnected(wdev->netdev,
866 						ev->dc.ie, ev->dc.ie_len,
867 						ev->dc.reason,
868 						!ev->dc.locally_generated);
869 			break;
870 		case EVENT_IBSS_JOINED:
871 			__cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
872 					       ev->ij.channel);
873 			break;
874 		case EVENT_STOPPED:
875 			__cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
876 			break;
877 		case EVENT_PORT_AUTHORIZED:
878 			__cfg80211_port_authorized(wdev, ev->pa.bssid);
879 			break;
880 		}
881 		wdev_unlock(wdev);
882 
883 		kfree(ev);
884 
885 		spin_lock_irqsave(&wdev->event_lock, flags);
886 	}
887 	spin_unlock_irqrestore(&wdev->event_lock, flags);
888 }
889 
890 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
891 {
892 	struct wireless_dev *wdev;
893 
894 	ASSERT_RTNL();
895 
896 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
897 		cfg80211_process_wdev_events(wdev);
898 }
899 
900 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
901 			  struct net_device *dev, enum nl80211_iftype ntype,
902 			  struct vif_params *params)
903 {
904 	int err;
905 	enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
906 
907 	ASSERT_RTNL();
908 
909 	/* don't support changing VLANs, you just re-create them */
910 	if (otype == NL80211_IFTYPE_AP_VLAN)
911 		return -EOPNOTSUPP;
912 
913 	/* cannot change into P2P device or NAN */
914 	if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
915 	    ntype == NL80211_IFTYPE_NAN)
916 		return -EOPNOTSUPP;
917 
918 	if (!rdev->ops->change_virtual_intf ||
919 	    !(rdev->wiphy.interface_modes & (1 << ntype)))
920 		return -EOPNOTSUPP;
921 
922 	/* if it's part of a bridge, reject changing type to station/ibss */
923 	if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
924 	    (ntype == NL80211_IFTYPE_ADHOC ||
925 	     ntype == NL80211_IFTYPE_STATION ||
926 	     ntype == NL80211_IFTYPE_P2P_CLIENT))
927 		return -EBUSY;
928 
929 	if (ntype != otype) {
930 		dev->ieee80211_ptr->use_4addr = false;
931 		dev->ieee80211_ptr->mesh_id_up_len = 0;
932 		wdev_lock(dev->ieee80211_ptr);
933 		rdev_set_qos_map(rdev, dev, NULL);
934 		wdev_unlock(dev->ieee80211_ptr);
935 
936 		switch (otype) {
937 		case NL80211_IFTYPE_AP:
938 			cfg80211_stop_ap(rdev, dev, true);
939 			break;
940 		case NL80211_IFTYPE_ADHOC:
941 			cfg80211_leave_ibss(rdev, dev, false);
942 			break;
943 		case NL80211_IFTYPE_STATION:
944 		case NL80211_IFTYPE_P2P_CLIENT:
945 			wdev_lock(dev->ieee80211_ptr);
946 			cfg80211_disconnect(rdev, dev,
947 					    WLAN_REASON_DEAUTH_LEAVING, true);
948 			wdev_unlock(dev->ieee80211_ptr);
949 			break;
950 		case NL80211_IFTYPE_MESH_POINT:
951 			/* mesh should be handled? */
952 			break;
953 		default:
954 			break;
955 		}
956 
957 		cfg80211_process_rdev_events(rdev);
958 	}
959 
960 	err = rdev_change_virtual_intf(rdev, dev, ntype, params);
961 
962 	WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
963 
964 	if (!err && params && params->use_4addr != -1)
965 		dev->ieee80211_ptr->use_4addr = params->use_4addr;
966 
967 	if (!err) {
968 		dev->priv_flags &= ~IFF_DONT_BRIDGE;
969 		switch (ntype) {
970 		case NL80211_IFTYPE_STATION:
971 			if (dev->ieee80211_ptr->use_4addr)
972 				break;
973 			/* fall through */
974 		case NL80211_IFTYPE_OCB:
975 		case NL80211_IFTYPE_P2P_CLIENT:
976 		case NL80211_IFTYPE_ADHOC:
977 			dev->priv_flags |= IFF_DONT_BRIDGE;
978 			break;
979 		case NL80211_IFTYPE_P2P_GO:
980 		case NL80211_IFTYPE_AP:
981 		case NL80211_IFTYPE_AP_VLAN:
982 		case NL80211_IFTYPE_WDS:
983 		case NL80211_IFTYPE_MESH_POINT:
984 			/* bridging OK */
985 			break;
986 		case NL80211_IFTYPE_MONITOR:
987 			/* monitor can't bridge anyway */
988 			break;
989 		case NL80211_IFTYPE_UNSPECIFIED:
990 		case NUM_NL80211_IFTYPES:
991 			/* not happening */
992 			break;
993 		case NL80211_IFTYPE_P2P_DEVICE:
994 		case NL80211_IFTYPE_NAN:
995 			WARN_ON(1);
996 			break;
997 		}
998 	}
999 
1000 	if (!err && ntype != otype && netif_running(dev)) {
1001 		cfg80211_update_iface_num(rdev, ntype, 1);
1002 		cfg80211_update_iface_num(rdev, otype, -1);
1003 	}
1004 
1005 	return err;
1006 }
1007 
1008 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1009 {
1010 	int modulation, streams, bitrate;
1011 
1012 	/* the formula below does only work for MCS values smaller than 32 */
1013 	if (WARN_ON_ONCE(rate->mcs >= 32))
1014 		return 0;
1015 
1016 	modulation = rate->mcs & 7;
1017 	streams = (rate->mcs >> 3) + 1;
1018 
1019 	bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1020 
1021 	if (modulation < 4)
1022 		bitrate *= (modulation + 1);
1023 	else if (modulation == 4)
1024 		bitrate *= (modulation + 2);
1025 	else
1026 		bitrate *= (modulation + 3);
1027 
1028 	bitrate *= streams;
1029 
1030 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1031 		bitrate = (bitrate / 9) * 10;
1032 
1033 	/* do NOT round down here */
1034 	return (bitrate + 50000) / 100000;
1035 }
1036 
1037 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1038 {
1039 	static const u32 __mcs2bitrate[] = {
1040 		/* control PHY */
1041 		[0] =   275,
1042 		/* SC PHY */
1043 		[1] =  3850,
1044 		[2] =  7700,
1045 		[3] =  9625,
1046 		[4] = 11550,
1047 		[5] = 12512, /* 1251.25 mbps */
1048 		[6] = 15400,
1049 		[7] = 19250,
1050 		[8] = 23100,
1051 		[9] = 25025,
1052 		[10] = 30800,
1053 		[11] = 38500,
1054 		[12] = 46200,
1055 		/* OFDM PHY */
1056 		[13] =  6930,
1057 		[14] =  8662, /* 866.25 mbps */
1058 		[15] = 13860,
1059 		[16] = 17325,
1060 		[17] = 20790,
1061 		[18] = 27720,
1062 		[19] = 34650,
1063 		[20] = 41580,
1064 		[21] = 45045,
1065 		[22] = 51975,
1066 		[23] = 62370,
1067 		[24] = 67568, /* 6756.75 mbps */
1068 		/* LP-SC PHY */
1069 		[25] =  6260,
1070 		[26] =  8340,
1071 		[27] = 11120,
1072 		[28] = 12510,
1073 		[29] = 16680,
1074 		[30] = 22240,
1075 		[31] = 25030,
1076 	};
1077 
1078 	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1079 		return 0;
1080 
1081 	return __mcs2bitrate[rate->mcs];
1082 }
1083 
1084 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1085 {
1086 	static const u32 base[4][10] = {
1087 		{   6500000,
1088 		   13000000,
1089 		   19500000,
1090 		   26000000,
1091 		   39000000,
1092 		   52000000,
1093 		   58500000,
1094 		   65000000,
1095 		   78000000,
1096 		/* not in the spec, but some devices use this: */
1097 		   86500000,
1098 		},
1099 		{  13500000,
1100 		   27000000,
1101 		   40500000,
1102 		   54000000,
1103 		   81000000,
1104 		  108000000,
1105 		  121500000,
1106 		  135000000,
1107 		  162000000,
1108 		  180000000,
1109 		},
1110 		{  29300000,
1111 		   58500000,
1112 		   87800000,
1113 		  117000000,
1114 		  175500000,
1115 		  234000000,
1116 		  263300000,
1117 		  292500000,
1118 		  351000000,
1119 		  390000000,
1120 		},
1121 		{  58500000,
1122 		  117000000,
1123 		  175500000,
1124 		  234000000,
1125 		  351000000,
1126 		  468000000,
1127 		  526500000,
1128 		  585000000,
1129 		  702000000,
1130 		  780000000,
1131 		},
1132 	};
1133 	u32 bitrate;
1134 	int idx;
1135 
1136 	if (rate->mcs > 9)
1137 		goto warn;
1138 
1139 	switch (rate->bw) {
1140 	case RATE_INFO_BW_160:
1141 		idx = 3;
1142 		break;
1143 	case RATE_INFO_BW_80:
1144 		idx = 2;
1145 		break;
1146 	case RATE_INFO_BW_40:
1147 		idx = 1;
1148 		break;
1149 	case RATE_INFO_BW_5:
1150 	case RATE_INFO_BW_10:
1151 	default:
1152 		goto warn;
1153 	case RATE_INFO_BW_20:
1154 		idx = 0;
1155 	}
1156 
1157 	bitrate = base[idx][rate->mcs];
1158 	bitrate *= rate->nss;
1159 
1160 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1161 		bitrate = (bitrate / 9) * 10;
1162 
1163 	/* do NOT round down here */
1164 	return (bitrate + 50000) / 100000;
1165  warn:
1166 	WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1167 		  rate->bw, rate->mcs, rate->nss);
1168 	return 0;
1169 }
1170 
1171 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1172 {
1173 #define SCALE 2048
1174 	u16 mcs_divisors[12] = {
1175 		34133, /* 16.666666... */
1176 		17067, /*  8.333333... */
1177 		11378, /*  5.555555... */
1178 		 8533, /*  4.166666... */
1179 		 5689, /*  2.777777... */
1180 		 4267, /*  2.083333... */
1181 		 3923, /*  1.851851... */
1182 		 3413, /*  1.666666... */
1183 		 2844, /*  1.388888... */
1184 		 2560, /*  1.250000... */
1185 		 2276, /*  1.111111... */
1186 		 2048, /*  1.000000... */
1187 	};
1188 	u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1189 	u32 rates_969[3] =  { 480388888, 453700000, 408333333 };
1190 	u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1191 	u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1192 	u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1193 	u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1194 	u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1195 	u64 tmp;
1196 	u32 result;
1197 
1198 	if (WARN_ON_ONCE(rate->mcs > 11))
1199 		return 0;
1200 
1201 	if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1202 		return 0;
1203 	if (WARN_ON_ONCE(rate->he_ru_alloc >
1204 			 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1205 		return 0;
1206 	if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1207 		return 0;
1208 
1209 	if (rate->bw == RATE_INFO_BW_160)
1210 		result = rates_160M[rate->he_gi];
1211 	else if (rate->bw == RATE_INFO_BW_80 ||
1212 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1213 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1214 		result = rates_969[rate->he_gi];
1215 	else if (rate->bw == RATE_INFO_BW_40 ||
1216 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1217 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1218 		result = rates_484[rate->he_gi];
1219 	else if (rate->bw == RATE_INFO_BW_20 ||
1220 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1221 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1222 		result = rates_242[rate->he_gi];
1223 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1224 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1225 		result = rates_106[rate->he_gi];
1226 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1227 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1228 		result = rates_52[rate->he_gi];
1229 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1230 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1231 		result = rates_26[rate->he_gi];
1232 	else {
1233 		WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1234 		     rate->bw, rate->he_ru_alloc);
1235 		return 0;
1236 	}
1237 
1238 	/* now scale to the appropriate MCS */
1239 	tmp = result;
1240 	tmp *= SCALE;
1241 	do_div(tmp, mcs_divisors[rate->mcs]);
1242 	result = tmp;
1243 
1244 	/* and take NSS, DCM into account */
1245 	result = (result * rate->nss) / 8;
1246 	if (rate->he_dcm)
1247 		result /= 2;
1248 
1249 	return result / 10000;
1250 }
1251 
1252 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1253 {
1254 	if (rate->flags & RATE_INFO_FLAGS_MCS)
1255 		return cfg80211_calculate_bitrate_ht(rate);
1256 	if (rate->flags & RATE_INFO_FLAGS_60G)
1257 		return cfg80211_calculate_bitrate_60g(rate);
1258 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1259 		return cfg80211_calculate_bitrate_vht(rate);
1260 	if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1261 		return cfg80211_calculate_bitrate_he(rate);
1262 
1263 	return rate->legacy;
1264 }
1265 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1266 
1267 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1268 			  enum ieee80211_p2p_attr_id attr,
1269 			  u8 *buf, unsigned int bufsize)
1270 {
1271 	u8 *out = buf;
1272 	u16 attr_remaining = 0;
1273 	bool desired_attr = false;
1274 	u16 desired_len = 0;
1275 
1276 	while (len > 0) {
1277 		unsigned int iedatalen;
1278 		unsigned int copy;
1279 		const u8 *iedata;
1280 
1281 		if (len < 2)
1282 			return -EILSEQ;
1283 		iedatalen = ies[1];
1284 		if (iedatalen + 2 > len)
1285 			return -EILSEQ;
1286 
1287 		if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1288 			goto cont;
1289 
1290 		if (iedatalen < 4)
1291 			goto cont;
1292 
1293 		iedata = ies + 2;
1294 
1295 		/* check WFA OUI, P2P subtype */
1296 		if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1297 		    iedata[2] != 0x9a || iedata[3] != 0x09)
1298 			goto cont;
1299 
1300 		iedatalen -= 4;
1301 		iedata += 4;
1302 
1303 		/* check attribute continuation into this IE */
1304 		copy = min_t(unsigned int, attr_remaining, iedatalen);
1305 		if (copy && desired_attr) {
1306 			desired_len += copy;
1307 			if (out) {
1308 				memcpy(out, iedata, min(bufsize, copy));
1309 				out += min(bufsize, copy);
1310 				bufsize -= min(bufsize, copy);
1311 			}
1312 
1313 
1314 			if (copy == attr_remaining)
1315 				return desired_len;
1316 		}
1317 
1318 		attr_remaining -= copy;
1319 		if (attr_remaining)
1320 			goto cont;
1321 
1322 		iedatalen -= copy;
1323 		iedata += copy;
1324 
1325 		while (iedatalen > 0) {
1326 			u16 attr_len;
1327 
1328 			/* P2P attribute ID & size must fit */
1329 			if (iedatalen < 3)
1330 				return -EILSEQ;
1331 			desired_attr = iedata[0] == attr;
1332 			attr_len = get_unaligned_le16(iedata + 1);
1333 			iedatalen -= 3;
1334 			iedata += 3;
1335 
1336 			copy = min_t(unsigned int, attr_len, iedatalen);
1337 
1338 			if (desired_attr) {
1339 				desired_len += copy;
1340 				if (out) {
1341 					memcpy(out, iedata, min(bufsize, copy));
1342 					out += min(bufsize, copy);
1343 					bufsize -= min(bufsize, copy);
1344 				}
1345 
1346 				if (copy == attr_len)
1347 					return desired_len;
1348 			}
1349 
1350 			iedata += copy;
1351 			iedatalen -= copy;
1352 			attr_remaining = attr_len - copy;
1353 		}
1354 
1355  cont:
1356 		len -= ies[1] + 2;
1357 		ies += ies[1] + 2;
1358 	}
1359 
1360 	if (attr_remaining && desired_attr)
1361 		return -EILSEQ;
1362 
1363 	return -ENOENT;
1364 }
1365 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1366 
1367 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1368 {
1369 	int i;
1370 
1371 	/* Make sure array values are legal */
1372 	if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1373 		return false;
1374 
1375 	i = 0;
1376 	while (i < n_ids) {
1377 		if (ids[i] == WLAN_EID_EXTENSION) {
1378 			if (id_ext && (ids[i + 1] == id))
1379 				return true;
1380 
1381 			i += 2;
1382 			continue;
1383 		}
1384 
1385 		if (ids[i] == id && !id_ext)
1386 			return true;
1387 
1388 		i++;
1389 	}
1390 	return false;
1391 }
1392 
1393 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1394 {
1395 	/* we assume a validly formed IEs buffer */
1396 	u8 len = ies[pos + 1];
1397 
1398 	pos += 2 + len;
1399 
1400 	/* the IE itself must have 255 bytes for fragments to follow */
1401 	if (len < 255)
1402 		return pos;
1403 
1404 	while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1405 		len = ies[pos + 1];
1406 		pos += 2 + len;
1407 	}
1408 
1409 	return pos;
1410 }
1411 
1412 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1413 			      const u8 *ids, int n_ids,
1414 			      const u8 *after_ric, int n_after_ric,
1415 			      size_t offset)
1416 {
1417 	size_t pos = offset;
1418 
1419 	while (pos < ielen) {
1420 		u8 ext = 0;
1421 
1422 		if (ies[pos] == WLAN_EID_EXTENSION)
1423 			ext = 2;
1424 		if ((pos + ext) >= ielen)
1425 			break;
1426 
1427 		if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1428 					  ies[pos] == WLAN_EID_EXTENSION))
1429 			break;
1430 
1431 		if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1432 			pos = skip_ie(ies, ielen, pos);
1433 
1434 			while (pos < ielen) {
1435 				if (ies[pos] == WLAN_EID_EXTENSION)
1436 					ext = 2;
1437 				else
1438 					ext = 0;
1439 
1440 				if ((pos + ext) >= ielen)
1441 					break;
1442 
1443 				if (!ieee80211_id_in_list(after_ric,
1444 							  n_after_ric,
1445 							  ies[pos + ext],
1446 							  ext == 2))
1447 					pos = skip_ie(ies, ielen, pos);
1448 				else
1449 					break;
1450 			}
1451 		} else {
1452 			pos = skip_ie(ies, ielen, pos);
1453 		}
1454 	}
1455 
1456 	return pos;
1457 }
1458 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1459 
1460 bool ieee80211_operating_class_to_band(u8 operating_class,
1461 				       enum nl80211_band *band)
1462 {
1463 	switch (operating_class) {
1464 	case 112:
1465 	case 115 ... 127:
1466 	case 128 ... 130:
1467 		*band = NL80211_BAND_5GHZ;
1468 		return true;
1469 	case 81:
1470 	case 82:
1471 	case 83:
1472 	case 84:
1473 		*band = NL80211_BAND_2GHZ;
1474 		return true;
1475 	case 180:
1476 		*band = NL80211_BAND_60GHZ;
1477 		return true;
1478 	}
1479 
1480 	return false;
1481 }
1482 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1483 
1484 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1485 					  u8 *op_class)
1486 {
1487 	u8 vht_opclass;
1488 	u32 freq = chandef->center_freq1;
1489 
1490 	if (freq >= 2412 && freq <= 2472) {
1491 		if (chandef->width > NL80211_CHAN_WIDTH_40)
1492 			return false;
1493 
1494 		/* 2.407 GHz, channels 1..13 */
1495 		if (chandef->width == NL80211_CHAN_WIDTH_40) {
1496 			if (freq > chandef->chan->center_freq)
1497 				*op_class = 83; /* HT40+ */
1498 			else
1499 				*op_class = 84; /* HT40- */
1500 		} else {
1501 			*op_class = 81;
1502 		}
1503 
1504 		return true;
1505 	}
1506 
1507 	if (freq == 2484) {
1508 		if (chandef->width > NL80211_CHAN_WIDTH_40)
1509 			return false;
1510 
1511 		*op_class = 82; /* channel 14 */
1512 		return true;
1513 	}
1514 
1515 	switch (chandef->width) {
1516 	case NL80211_CHAN_WIDTH_80:
1517 		vht_opclass = 128;
1518 		break;
1519 	case NL80211_CHAN_WIDTH_160:
1520 		vht_opclass = 129;
1521 		break;
1522 	case NL80211_CHAN_WIDTH_80P80:
1523 		vht_opclass = 130;
1524 		break;
1525 	case NL80211_CHAN_WIDTH_10:
1526 	case NL80211_CHAN_WIDTH_5:
1527 		return false; /* unsupported for now */
1528 	default:
1529 		vht_opclass = 0;
1530 		break;
1531 	}
1532 
1533 	/* 5 GHz, channels 36..48 */
1534 	if (freq >= 5180 && freq <= 5240) {
1535 		if (vht_opclass) {
1536 			*op_class = vht_opclass;
1537 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1538 			if (freq > chandef->chan->center_freq)
1539 				*op_class = 116;
1540 			else
1541 				*op_class = 117;
1542 		} else {
1543 			*op_class = 115;
1544 		}
1545 
1546 		return true;
1547 	}
1548 
1549 	/* 5 GHz, channels 52..64 */
1550 	if (freq >= 5260 && freq <= 5320) {
1551 		if (vht_opclass) {
1552 			*op_class = vht_opclass;
1553 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1554 			if (freq > chandef->chan->center_freq)
1555 				*op_class = 119;
1556 			else
1557 				*op_class = 120;
1558 		} else {
1559 			*op_class = 118;
1560 		}
1561 
1562 		return true;
1563 	}
1564 
1565 	/* 5 GHz, channels 100..144 */
1566 	if (freq >= 5500 && freq <= 5720) {
1567 		if (vht_opclass) {
1568 			*op_class = vht_opclass;
1569 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1570 			if (freq > chandef->chan->center_freq)
1571 				*op_class = 122;
1572 			else
1573 				*op_class = 123;
1574 		} else {
1575 			*op_class = 121;
1576 		}
1577 
1578 		return true;
1579 	}
1580 
1581 	/* 5 GHz, channels 149..169 */
1582 	if (freq >= 5745 && freq <= 5845) {
1583 		if (vht_opclass) {
1584 			*op_class = vht_opclass;
1585 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1586 			if (freq > chandef->chan->center_freq)
1587 				*op_class = 126;
1588 			else
1589 				*op_class = 127;
1590 		} else if (freq <= 5805) {
1591 			*op_class = 124;
1592 		} else {
1593 			*op_class = 125;
1594 		}
1595 
1596 		return true;
1597 	}
1598 
1599 	/* 56.16 GHz, channel 1..4 */
1600 	if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1601 		if (chandef->width >= NL80211_CHAN_WIDTH_40)
1602 			return false;
1603 
1604 		*op_class = 180;
1605 		return true;
1606 	}
1607 
1608 	/* not supported yet */
1609 	return false;
1610 }
1611 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1612 
1613 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1614 				       u32 *beacon_int_gcd,
1615 				       bool *beacon_int_different)
1616 {
1617 	struct wireless_dev *wdev;
1618 
1619 	*beacon_int_gcd = 0;
1620 	*beacon_int_different = false;
1621 
1622 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1623 		if (!wdev->beacon_interval)
1624 			continue;
1625 
1626 		if (!*beacon_int_gcd) {
1627 			*beacon_int_gcd = wdev->beacon_interval;
1628 			continue;
1629 		}
1630 
1631 		if (wdev->beacon_interval == *beacon_int_gcd)
1632 			continue;
1633 
1634 		*beacon_int_different = true;
1635 		*beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1636 	}
1637 
1638 	if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1639 		if (*beacon_int_gcd)
1640 			*beacon_int_different = true;
1641 		*beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1642 	}
1643 }
1644 
1645 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1646 				 enum nl80211_iftype iftype, u32 beacon_int)
1647 {
1648 	/*
1649 	 * This is just a basic pre-condition check; if interface combinations
1650 	 * are possible the driver must already be checking those with a call
1651 	 * to cfg80211_check_combinations(), in which case we'll validate more
1652 	 * through the cfg80211_calculate_bi_data() call and code in
1653 	 * cfg80211_iter_combinations().
1654 	 */
1655 
1656 	if (beacon_int < 10 || beacon_int > 10000)
1657 		return -EINVAL;
1658 
1659 	return 0;
1660 }
1661 
1662 int cfg80211_iter_combinations(struct wiphy *wiphy,
1663 			       struct iface_combination_params *params,
1664 			       void (*iter)(const struct ieee80211_iface_combination *c,
1665 					    void *data),
1666 			       void *data)
1667 {
1668 	const struct ieee80211_regdomain *regdom;
1669 	enum nl80211_dfs_regions region = 0;
1670 	int i, j, iftype;
1671 	int num_interfaces = 0;
1672 	u32 used_iftypes = 0;
1673 	u32 beacon_int_gcd;
1674 	bool beacon_int_different;
1675 
1676 	/*
1677 	 * This is a bit strange, since the iteration used to rely only on
1678 	 * the data given by the driver, but here it now relies on context,
1679 	 * in form of the currently operating interfaces.
1680 	 * This is OK for all current users, and saves us from having to
1681 	 * push the GCD calculations into all the drivers.
1682 	 * In the future, this should probably rely more on data that's in
1683 	 * cfg80211 already - the only thing not would appear to be any new
1684 	 * interfaces (while being brought up) and channel/radar data.
1685 	 */
1686 	cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1687 				   &beacon_int_gcd, &beacon_int_different);
1688 
1689 	if (params->radar_detect) {
1690 		rcu_read_lock();
1691 		regdom = rcu_dereference(cfg80211_regdomain);
1692 		if (regdom)
1693 			region = regdom->dfs_region;
1694 		rcu_read_unlock();
1695 	}
1696 
1697 	for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1698 		num_interfaces += params->iftype_num[iftype];
1699 		if (params->iftype_num[iftype] > 0 &&
1700 		    !(wiphy->software_iftypes & BIT(iftype)))
1701 			used_iftypes |= BIT(iftype);
1702 	}
1703 
1704 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1705 		const struct ieee80211_iface_combination *c;
1706 		struct ieee80211_iface_limit *limits;
1707 		u32 all_iftypes = 0;
1708 
1709 		c = &wiphy->iface_combinations[i];
1710 
1711 		if (num_interfaces > c->max_interfaces)
1712 			continue;
1713 		if (params->num_different_channels > c->num_different_channels)
1714 			continue;
1715 
1716 		limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1717 				 GFP_KERNEL);
1718 		if (!limits)
1719 			return -ENOMEM;
1720 
1721 		for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1722 			if (wiphy->software_iftypes & BIT(iftype))
1723 				continue;
1724 			for (j = 0; j < c->n_limits; j++) {
1725 				all_iftypes |= limits[j].types;
1726 				if (!(limits[j].types & BIT(iftype)))
1727 					continue;
1728 				if (limits[j].max < params->iftype_num[iftype])
1729 					goto cont;
1730 				limits[j].max -= params->iftype_num[iftype];
1731 			}
1732 		}
1733 
1734 		if (params->radar_detect !=
1735 			(c->radar_detect_widths & params->radar_detect))
1736 			goto cont;
1737 
1738 		if (params->radar_detect && c->radar_detect_regions &&
1739 		    !(c->radar_detect_regions & BIT(region)))
1740 			goto cont;
1741 
1742 		/* Finally check that all iftypes that we're currently
1743 		 * using are actually part of this combination. If they
1744 		 * aren't then we can't use this combination and have
1745 		 * to continue to the next.
1746 		 */
1747 		if ((all_iftypes & used_iftypes) != used_iftypes)
1748 			goto cont;
1749 
1750 		if (beacon_int_gcd) {
1751 			if (c->beacon_int_min_gcd &&
1752 			    beacon_int_gcd < c->beacon_int_min_gcd)
1753 				goto cont;
1754 			if (!c->beacon_int_min_gcd && beacon_int_different)
1755 				goto cont;
1756 		}
1757 
1758 		/* This combination covered all interface types and
1759 		 * supported the requested numbers, so we're good.
1760 		 */
1761 
1762 		(*iter)(c, data);
1763  cont:
1764 		kfree(limits);
1765 	}
1766 
1767 	return 0;
1768 }
1769 EXPORT_SYMBOL(cfg80211_iter_combinations);
1770 
1771 static void
1772 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1773 			  void *data)
1774 {
1775 	int *num = data;
1776 	(*num)++;
1777 }
1778 
1779 int cfg80211_check_combinations(struct wiphy *wiphy,
1780 				struct iface_combination_params *params)
1781 {
1782 	int err, num = 0;
1783 
1784 	err = cfg80211_iter_combinations(wiphy, params,
1785 					 cfg80211_iter_sum_ifcombs, &num);
1786 	if (err)
1787 		return err;
1788 	if (num == 0)
1789 		return -EBUSY;
1790 
1791 	return 0;
1792 }
1793 EXPORT_SYMBOL(cfg80211_check_combinations);
1794 
1795 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1796 			   const u8 *rates, unsigned int n_rates,
1797 			   u32 *mask)
1798 {
1799 	int i, j;
1800 
1801 	if (!sband)
1802 		return -EINVAL;
1803 
1804 	if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1805 		return -EINVAL;
1806 
1807 	*mask = 0;
1808 
1809 	for (i = 0; i < n_rates; i++) {
1810 		int rate = (rates[i] & 0x7f) * 5;
1811 		bool found = false;
1812 
1813 		for (j = 0; j < sband->n_bitrates; j++) {
1814 			if (sband->bitrates[j].bitrate == rate) {
1815 				found = true;
1816 				*mask |= BIT(j);
1817 				break;
1818 			}
1819 		}
1820 		if (!found)
1821 			return -EINVAL;
1822 	}
1823 
1824 	/*
1825 	 * mask must have at least one bit set here since we
1826 	 * didn't accept a 0-length rates array nor allowed
1827 	 * entries in the array that didn't exist
1828 	 */
1829 
1830 	return 0;
1831 }
1832 
1833 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1834 {
1835 	enum nl80211_band band;
1836 	unsigned int n_channels = 0;
1837 
1838 	for (band = 0; band < NUM_NL80211_BANDS; band++)
1839 		if (wiphy->bands[band])
1840 			n_channels += wiphy->bands[band]->n_channels;
1841 
1842 	return n_channels;
1843 }
1844 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1845 
1846 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1847 			 struct station_info *sinfo)
1848 {
1849 	struct cfg80211_registered_device *rdev;
1850 	struct wireless_dev *wdev;
1851 
1852 	wdev = dev->ieee80211_ptr;
1853 	if (!wdev)
1854 		return -EOPNOTSUPP;
1855 
1856 	rdev = wiphy_to_rdev(wdev->wiphy);
1857 	if (!rdev->ops->get_station)
1858 		return -EOPNOTSUPP;
1859 
1860 	memset(sinfo, 0, sizeof(*sinfo));
1861 
1862 	return rdev_get_station(rdev, dev, mac_addr, sinfo);
1863 }
1864 EXPORT_SYMBOL(cfg80211_get_station);
1865 
1866 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1867 {
1868 	int i;
1869 
1870 	if (!f)
1871 		return;
1872 
1873 	kfree(f->serv_spec_info);
1874 	kfree(f->srf_bf);
1875 	kfree(f->srf_macs);
1876 	for (i = 0; i < f->num_rx_filters; i++)
1877 		kfree(f->rx_filters[i].filter);
1878 
1879 	for (i = 0; i < f->num_tx_filters; i++)
1880 		kfree(f->tx_filters[i].filter);
1881 
1882 	kfree(f->rx_filters);
1883 	kfree(f->tx_filters);
1884 	kfree(f);
1885 }
1886 EXPORT_SYMBOL(cfg80211_free_nan_func);
1887 
1888 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1889 				u32 center_freq_khz, u32 bw_khz)
1890 {
1891 	u32 start_freq_khz, end_freq_khz;
1892 
1893 	start_freq_khz = center_freq_khz - (bw_khz / 2);
1894 	end_freq_khz = center_freq_khz + (bw_khz / 2);
1895 
1896 	if (start_freq_khz >= freq_range->start_freq_khz &&
1897 	    end_freq_khz <= freq_range->end_freq_khz)
1898 		return true;
1899 
1900 	return false;
1901 }
1902 
1903 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1904 {
1905 	sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1906 				sizeof(*(sinfo->pertid)),
1907 				gfp);
1908 	if (!sinfo->pertid)
1909 		return -ENOMEM;
1910 
1911 	return 0;
1912 }
1913 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1914 
1915 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1916 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1917 const unsigned char rfc1042_header[] __aligned(2) =
1918 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1919 EXPORT_SYMBOL(rfc1042_header);
1920 
1921 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1922 const unsigned char bridge_tunnel_header[] __aligned(2) =
1923 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1924 EXPORT_SYMBOL(bridge_tunnel_header);
1925 
1926 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1927 struct iapp_layer2_update {
1928 	u8 da[ETH_ALEN];	/* broadcast */
1929 	u8 sa[ETH_ALEN];	/* STA addr */
1930 	__be16 len;		/* 6 */
1931 	u8 dsap;		/* 0 */
1932 	u8 ssap;		/* 0 */
1933 	u8 control;
1934 	u8 xid_info[3];
1935 } __packed;
1936 
1937 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1938 {
1939 	struct iapp_layer2_update *msg;
1940 	struct sk_buff *skb;
1941 
1942 	/* Send Level 2 Update Frame to update forwarding tables in layer 2
1943 	 * bridge devices */
1944 
1945 	skb = dev_alloc_skb(sizeof(*msg));
1946 	if (!skb)
1947 		return;
1948 	msg = skb_put(skb, sizeof(*msg));
1949 
1950 	/* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1951 	 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1952 
1953 	eth_broadcast_addr(msg->da);
1954 	ether_addr_copy(msg->sa, addr);
1955 	msg->len = htons(6);
1956 	msg->dsap = 0;
1957 	msg->ssap = 0x01;	/* NULL LSAP, CR Bit: Response */
1958 	msg->control = 0xaf;	/* XID response lsb.1111F101.
1959 				 * F=0 (no poll command; unsolicited frame) */
1960 	msg->xid_info[0] = 0x81;	/* XID format identifier */
1961 	msg->xid_info[1] = 1;	/* LLC types/classes: Type 1 LLC */
1962 	msg->xid_info[2] = 0;	/* XID sender's receive window size (RW) */
1963 
1964 	skb->dev = dev;
1965 	skb->protocol = eth_type_trans(skb, dev);
1966 	memset(skb->cb, 0, sizeof(skb->cb));
1967 	netif_rx_ni(skb);
1968 }
1969 EXPORT_SYMBOL(cfg80211_send_layer2_update);
1970 
1971 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1972 			      enum ieee80211_vht_chanwidth bw,
1973 			      int mcs, bool ext_nss_bw_capable)
1974 {
1975 	u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1976 	int max_vht_nss = 0;
1977 	int ext_nss_bw;
1978 	int supp_width;
1979 	int i, mcs_encoding;
1980 
1981 	if (map == 0xffff)
1982 		return 0;
1983 
1984 	if (WARN_ON(mcs > 9))
1985 		return 0;
1986 	if (mcs <= 7)
1987 		mcs_encoding = 0;
1988 	else if (mcs == 8)
1989 		mcs_encoding = 1;
1990 	else
1991 		mcs_encoding = 2;
1992 
1993 	/* find max_vht_nss for the given MCS */
1994 	for (i = 7; i >= 0; i--) {
1995 		int supp = (map >> (2 * i)) & 3;
1996 
1997 		if (supp == 3)
1998 			continue;
1999 
2000 		if (supp >= mcs_encoding) {
2001 			max_vht_nss = i + 1;
2002 			break;
2003 		}
2004 	}
2005 
2006 	if (!(cap->supp_mcs.tx_mcs_map &
2007 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2008 		return max_vht_nss;
2009 
2010 	ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2011 				   IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2012 	supp_width = le32_get_bits(cap->vht_cap_info,
2013 				   IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2014 
2015 	/* if not capable, treat ext_nss_bw as 0 */
2016 	if (!ext_nss_bw_capable)
2017 		ext_nss_bw = 0;
2018 
2019 	/* This is invalid */
2020 	if (supp_width == 3)
2021 		return 0;
2022 
2023 	/* This is an invalid combination so pretend nothing is supported */
2024 	if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2025 		return 0;
2026 
2027 	/*
2028 	 * Cover all the special cases according to IEEE 802.11-2016
2029 	 * Table 9-250. All other cases are either factor of 1 or not
2030 	 * valid/supported.
2031 	 */
2032 	switch (bw) {
2033 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
2034 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
2035 		if ((supp_width == 1 || supp_width == 2) &&
2036 		    ext_nss_bw == 3)
2037 			return 2 * max_vht_nss;
2038 		break;
2039 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
2040 		if (supp_width == 0 &&
2041 		    (ext_nss_bw == 1 || ext_nss_bw == 2))
2042 			return max_vht_nss / 2;
2043 		if (supp_width == 0 &&
2044 		    ext_nss_bw == 3)
2045 			return (3 * max_vht_nss) / 4;
2046 		if (supp_width == 1 &&
2047 		    ext_nss_bw == 3)
2048 			return 2 * max_vht_nss;
2049 		break;
2050 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2051 		if (supp_width == 0 && ext_nss_bw == 1)
2052 			return 0; /* not possible */
2053 		if (supp_width == 0 &&
2054 		    ext_nss_bw == 2)
2055 			return max_vht_nss / 2;
2056 		if (supp_width == 0 &&
2057 		    ext_nss_bw == 3)
2058 			return (3 * max_vht_nss) / 4;
2059 		if (supp_width == 1 &&
2060 		    ext_nss_bw == 0)
2061 			return 0; /* not possible */
2062 		if (supp_width == 1 &&
2063 		    ext_nss_bw == 1)
2064 			return max_vht_nss / 2;
2065 		if (supp_width == 1 &&
2066 		    ext_nss_bw == 2)
2067 			return (3 * max_vht_nss) / 4;
2068 		break;
2069 	}
2070 
2071 	/* not covered or invalid combination received */
2072 	return max_vht_nss;
2073 }
2074 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2075