util.c 41.1 KB
Newer Older
1 2 3
/*
 * Wireless utility functions
 *
J
Johannes Berg 已提交
4
 * Copyright 2007-2009	Johannes Berg <johannes@sipsolutions.net>
5
 * Copyright 2013-2014  Intel Mobile Communications GmbH
6
 */
7
#include <linux/export.h>
J
Johannes Berg 已提交
8
#include <linux/bitops.h>
9
#include <linux/etherdevice.h>
10
#include <linux/slab.h>
J
Johannes Berg 已提交
11
#include <net/cfg80211.h>
12
#include <net/ip.h>
13
#include <net/dsfield.h>
14
#include <linux/if_vlan.h>
15
#include <linux/mpls.h>
16
#include "core.h"
17 18
#include "rdev-ops.h"

19

20 21
struct ieee80211_rate *
ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
22
			    u32 basic_rates, int bitrate)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
{
	struct ieee80211_rate *result = &sband->bitrates[0];
	int i;

	for (i = 0; i < sband->n_bitrates; i++) {
		if (!(basic_rates & BIT(i)))
			continue;
		if (sband->bitrates[i].bitrate > bitrate)
			continue;
		result = &sband->bitrates[i];
	}

	return result;
}
EXPORT_SYMBOL(ieee80211_get_response_rate);

39 40
u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
			      enum nl80211_bss_scan_width scan_width)
41 42 43 44 45 46 47 48 49
{
	struct ieee80211_rate *bitrates;
	u32 mandatory_rates = 0;
	enum ieee80211_rate_flags mandatory_flag;
	int i;

	if (WARN_ON(!sband))
		return 1;

50
	if (sband->band == NL80211_BAND_2GHZ) {
51 52 53 54 55 56
		if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
		    scan_width == NL80211_BSS_CHAN_WIDTH_10)
			mandatory_flag = IEEE80211_RATE_MANDATORY_G;
		else
			mandatory_flag = IEEE80211_RATE_MANDATORY_B;
	} else {
57
		mandatory_flag = IEEE80211_RATE_MANDATORY_A;
58
	}
59 60 61 62 63 64 65 66 67

	bitrates = sband->bitrates;
	for (i = 0; i < sband->n_bitrates; i++)
		if (bitrates[i].flags & mandatory_flag)
			mandatory_rates |= BIT(i);
	return mandatory_rates;
}
EXPORT_SYMBOL(ieee80211_mandatory_rates);

68
int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
69
{
70 71
	/* see 802.11 17.3.8.3.2 and Annex J
	 * there are overlapping channel numbers in 5GHz and 2GHz bands */
72 73 74
	if (chan <= 0)
		return 0; /* not supported */
	switch (band) {
75
	case NL80211_BAND_2GHZ:
76 77 78 79
		if (chan == 14)
			return 2484;
		else if (chan < 14)
			return 2407 + chan * 5;
80
		break;
81
	case NL80211_BAND_5GHZ:
82 83
		if (chan >= 182 && chan <= 196)
			return 4000 + chan * 5;
84
		else
85 86
			return 5000 + chan * 5;
		break;
87
	case NL80211_BAND_60GHZ:
88 89 90 91 92
		if (chan < 5)
			return 56160 + chan * 2160;
		break;
	default:
		;
93
	}
94
	return 0; /* not supported */
95 96 97 98 99
}
EXPORT_SYMBOL(ieee80211_channel_to_frequency);

int ieee80211_frequency_to_channel(int freq)
{
100
	/* see 802.11 17.3.8.3.2 and Annex J */
101 102
	if (freq == 2484)
		return 14;
103
	else if (freq < 2484)
104
		return (freq - 2407) / 5;
105 106
	else if (freq >= 4910 && freq <= 4980)
		return (freq - 4000) / 5;
107
	else if (freq <= 45000) /* DMG band lower limit */
108
		return (freq - 5000) / 5;
109 110 111 112
	else if (freq >= 58320 && freq <= 64800)
		return (freq - 56160) / 2160;
	else
		return 0;
113 114 115
}
EXPORT_SYMBOL(ieee80211_frequency_to_channel);

116 117
struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
						  int freq)
118
{
119
	enum nl80211_band band;
120 121 122
	struct ieee80211_supported_band *sband;
	int i;

123
	for (band = 0; band < NUM_NL80211_BANDS; band++) {
124 125 126 127 128 129 130 131 132 133 134 135 136
		sband = wiphy->bands[band];

		if (!sband)
			continue;

		for (i = 0; i < sband->n_channels; i++) {
			if (sband->channels[i].center_freq == freq)
				return &sband->channels[i];
		}
	}

	return NULL;
}
137
EXPORT_SYMBOL(__ieee80211_get_channel);
138

139
static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
140
				     enum nl80211_band band)
141 142 143 144
{
	int i, want;

	switch (band) {
145
	case NL80211_BAND_5GHZ:
146 147 148 149 150 151 152 153 154 155 156 157
		want = 3;
		for (i = 0; i < sband->n_bitrates; i++) {
			if (sband->bitrates[i].bitrate == 60 ||
			    sband->bitrates[i].bitrate == 120 ||
			    sband->bitrates[i].bitrate == 240) {
				sband->bitrates[i].flags |=
					IEEE80211_RATE_MANDATORY_A;
				want--;
			}
		}
		WARN_ON(want);
		break;
158
	case NL80211_BAND_2GHZ:
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
		want = 7;
		for (i = 0; i < sband->n_bitrates; i++) {
			if (sband->bitrates[i].bitrate == 10) {
				sband->bitrates[i].flags |=
					IEEE80211_RATE_MANDATORY_B |
					IEEE80211_RATE_MANDATORY_G;
				want--;
			}

			if (sband->bitrates[i].bitrate == 20 ||
			    sband->bitrates[i].bitrate == 55 ||
			    sband->bitrates[i].bitrate == 110 ||
			    sband->bitrates[i].bitrate == 60 ||
			    sband->bitrates[i].bitrate == 120 ||
			    sband->bitrates[i].bitrate == 240) {
				sband->bitrates[i].flags |=
					IEEE80211_RATE_MANDATORY_G;
				want--;
			}

J
Johannes Berg 已提交
179 180 181 182
			if (sband->bitrates[i].bitrate != 10 &&
			    sband->bitrates[i].bitrate != 20 &&
			    sband->bitrates[i].bitrate != 55 &&
			    sband->bitrates[i].bitrate != 110)
183 184 185
				sband->bitrates[i].flags |=
					IEEE80211_RATE_ERP_G;
		}
186
		WARN_ON(want != 0 && want != 3 && want != 6);
187
		break;
188
	case NL80211_BAND_60GHZ:
189 190 191 192
		/* check for mandatory HT MCS 1..4 */
		WARN_ON(!sband->ht_cap.ht_supported);
		WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
		break;
193
	case NUM_NL80211_BANDS:
194 195 196 197 198 199 200
		WARN_ON(1);
		break;
	}
}

void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
{
201
	enum nl80211_band band;
202

203
	for (band = 0; band < NUM_NL80211_BANDS; band++)
204 205 206
		if (wiphy->bands[band])
			set_mandatory_flags_band(wiphy->bands[band], band);
}
207

208 209 210 211 212 213 214 215 216
bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
{
	int i;
	for (i = 0; i < wiphy->n_cipher_suites; i++)
		if (cipher == wiphy->cipher_suites[i])
			return true;
	return false;
}

J
Johannes Berg 已提交
217 218
int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
				   struct key_params *params, int key_idx,
219
				   bool pairwise, const u8 *mac_addr)
220 221 222 223
{
	if (key_idx > 5)
		return -EINVAL;

224 225 226 227 228 229
	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
		return -EINVAL;

	if (pairwise && !mac_addr)
		return -EINVAL;

230 231 232
	switch (params->cipher) {
	case WLAN_CIPHER_SUITE_TKIP:
	case WLAN_CIPHER_SUITE_CCMP:
233 234 235
	case WLAN_CIPHER_SUITE_CCMP_256:
	case WLAN_CIPHER_SUITE_GCMP:
	case WLAN_CIPHER_SUITE_GCMP_256:
236 237 238 239 240 241 242 243 244 245
		/* Disallow pairwise keys with non-zero index unless it's WEP
		 * or a vendor specific cipher (because current deployments use
		 * pairwise WEP keys with non-zero indices and for vendor
		 * specific ciphers this should be validated in the driver or
		 * hardware level - but 802.11i clearly specifies to use zero)
		 */
		if (pairwise && key_idx)
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_AES_CMAC:
246 247 248
	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
249 250 251 252 253 254 255
		/* Disallow BIP (group-only) cipher as pairwise cipher */
		if (pairwise)
			return -EINVAL;
		break;
	default:
		break;
	}
256 257 258

	switch (params->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
J
Johannes Berg 已提交
259
		if (params->key_len != WLAN_KEY_LEN_WEP40)
260 261 262
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_TKIP:
J
Johannes Berg 已提交
263
		if (params->key_len != WLAN_KEY_LEN_TKIP)
264 265 266
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_CCMP:
J
Johannes Berg 已提交
267
		if (params->key_len != WLAN_KEY_LEN_CCMP)
268 269
			return -EINVAL;
		break;
270 271 272 273 274 275 276 277 278 279 280 281
	case WLAN_CIPHER_SUITE_CCMP_256:
		if (params->key_len != WLAN_KEY_LEN_CCMP_256)
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_GCMP:
		if (params->key_len != WLAN_KEY_LEN_GCMP)
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_GCMP_256:
		if (params->key_len != WLAN_KEY_LEN_GCMP_256)
			return -EINVAL;
		break;
282
	case WLAN_CIPHER_SUITE_WEP104:
J
Johannes Berg 已提交
283
		if (params->key_len != WLAN_KEY_LEN_WEP104)
284 285 286
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_AES_CMAC:
J
Johannes Berg 已提交
287
		if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
288 289
			return -EINVAL;
		break;
290 291 292 293 294 295 296 297 298 299 300 301
	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
		if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
			return -EINVAL;
		break;
	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
			return -EINVAL;
		break;
302
	default:
303 304 305 306 307 308 309 310
		/*
		 * We don't know anything about this algorithm,
		 * allow using it -- but the driver must check
		 * all parameters! We still check below whether
		 * or not the driver supports this algorithm,
		 * of course.
		 */
		break;
311 312
	}

313 314 315 316 317 318 319 320
	if (params->seq) {
		switch (params->cipher) {
		case WLAN_CIPHER_SUITE_WEP40:
		case WLAN_CIPHER_SUITE_WEP104:
			/* These ciphers do not use key sequence */
			return -EINVAL;
		case WLAN_CIPHER_SUITE_TKIP:
		case WLAN_CIPHER_SUITE_CCMP:
321 322 323
		case WLAN_CIPHER_SUITE_CCMP_256:
		case WLAN_CIPHER_SUITE_GCMP:
		case WLAN_CIPHER_SUITE_GCMP_256:
324
		case WLAN_CIPHER_SUITE_AES_CMAC:
325 326 327
		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
328 329 330 331 332 333
			if (params->seq_len != 6)
				return -EINVAL;
			break;
		}
	}

334
	if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
J
Johannes Berg 已提交
335 336
		return -EINVAL;

337 338
	return 0;
}
339

340
unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
341 342 343 344 345 346
{
	unsigned int hdrlen = 24;

	if (ieee80211_is_data(fc)) {
		if (ieee80211_has_a4(fc))
			hdrlen = 30;
347
		if (ieee80211_is_data_qos(fc)) {
348
			hdrlen += IEEE80211_QOS_CTL_LEN;
349 350 351
			if (ieee80211_has_order(fc))
				hdrlen += IEEE80211_HT_CTL_LEN;
		}
352 353 354
		goto out;
	}

355 356 357 358 359 360
	if (ieee80211_is_mgmt(fc)) {
		if (ieee80211_has_order(fc))
			hdrlen += IEEE80211_HT_CTL_LEN;
		goto out;
	}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	if (ieee80211_is_ctl(fc)) {
		/*
		 * ACK and CTS are 10 bytes, all others 16. To see how
		 * to get this condition consider
		 *   subtype mask:   0b0000000011110000 (0x00F0)
		 *   ACK subtype:    0b0000000011010000 (0x00D0)
		 *   CTS subtype:    0b0000000011000000 (0x00C0)
		 *   bits that matter:         ^^^      (0x00E0)
		 *   value of those: 0b0000000011000000 (0x00C0)
		 */
		if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
			hdrlen = 10;
		else
			hdrlen = 16;
	}
out:
	return hdrlen;
}
EXPORT_SYMBOL(ieee80211_hdrlen);

unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
{
	const struct ieee80211_hdr *hdr =
			(const struct ieee80211_hdr *)skb->data;
	unsigned int hdrlen;

	if (unlikely(skb->len < 10))
		return 0;
	hdrlen = ieee80211_hdrlen(hdr->frame_control);
	if (unlikely(hdrlen > skb->len))
		return 0;
	return hdrlen;
}
EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);

396
static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
397
{
398
	int ae = flags & MESH_FLAGS_AE;
399
	/* 802.11-2012, 8.2.4.7.3 */
400
	switch (ae) {
401
	default:
402 403
	case 0:
		return 6;
404
	case MESH_FLAGS_AE_A4:
405
		return 12;
406
	case MESH_FLAGS_AE_A5_A6:
407 408 409
		return 18;
	}
}
410 411 412 413 414

unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
{
	return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
}
415
EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
416

417 418
static int __ieee80211_data_to_8023(struct sk_buff *skb, struct ethhdr *ehdr,
				    const u8 *addr, enum nl80211_iftype iftype)
419 420
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
421 422 423 424 425 426 427
	struct {
		u8 hdr[ETH_ALEN] __aligned(2);
		__be16 proto;
	} payload;
	struct ethhdr tmp;
	u16 hdrlen;
	u8 mesh_flags = 0;
428 429 430 431 432

	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
		return -1;

	hdrlen = ieee80211_hdrlen(hdr->frame_control);
433 434
	if (skb->len < hdrlen + 8)
		return -1;
435 436 437 438 439 440 441 442 443 444

	/* convert IEEE 802.11 header + possible LLC headers into Ethernet
	 * header
	 * IEEE 802.11 address fields:
	 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
	 *   0     0   DA    SA    BSSID n/a
	 *   0     1   DA    BSSID SA    n/a
	 *   1     0   BSSID SA    DA    n/a
	 *   1     1   RA    TA    DA    SA
	 */
445 446 447 448 449
	memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
	memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);

	if (iftype == NL80211_IFTYPE_MESH_POINT)
		skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
450 451 452 453 454

	switch (hdr->frame_control &
		cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
	case cpu_to_le16(IEEE80211_FCTL_TODS):
		if (unlikely(iftype != NL80211_IFTYPE_AP &&
455 456
			     iftype != NL80211_IFTYPE_AP_VLAN &&
			     iftype != NL80211_IFTYPE_P2P_GO))
457 458 459 460
			return -1;
		break;
	case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
		if (unlikely(iftype != NL80211_IFTYPE_WDS &&
461 462 463
			     iftype != NL80211_IFTYPE_MESH_POINT &&
			     iftype != NL80211_IFTYPE_AP_VLAN &&
			     iftype != NL80211_IFTYPE_STATION))
464 465
			return -1;
		if (iftype == NL80211_IFTYPE_MESH_POINT) {
466
			if (mesh_flags & MESH_FLAGS_AE_A4)
Z
Zhu Yi 已提交
467
				return -1;
468
			if (mesh_flags & MESH_FLAGS_AE_A5_A6) {
Z
Zhu Yi 已提交
469 470
				skb_copy_bits(skb, hdrlen +
					offsetof(struct ieee80211s_hdr, eaddr1),
471
					tmp.h_dest, 2 * ETH_ALEN);
472
			}
473
			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
474 475 476
		}
		break;
	case cpu_to_le16(IEEE80211_FCTL_FROMDS):
477
		if ((iftype != NL80211_IFTYPE_STATION &&
478 479
		     iftype != NL80211_IFTYPE_P2P_CLIENT &&
		     iftype != NL80211_IFTYPE_MESH_POINT) ||
480 481
		    (is_multicast_ether_addr(tmp.h_dest) &&
		     ether_addr_equal(tmp.h_source, addr)))
482
			return -1;
483
		if (iftype == NL80211_IFTYPE_MESH_POINT) {
484
			if (mesh_flags & MESH_FLAGS_AE_A5_A6)
485
				return -1;
486
			if (mesh_flags & MESH_FLAGS_AE_A4)
Z
Zhu Yi 已提交
487 488
				skb_copy_bits(skb, hdrlen +
					offsetof(struct ieee80211s_hdr, eaddr1),
489 490
					tmp.h_source, ETH_ALEN);
			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
491
		}
492 493
		break;
	case cpu_to_le16(0):
494
		if (iftype != NL80211_IFTYPE_ADHOC &&
495 496
		    iftype != NL80211_IFTYPE_STATION &&
		    iftype != NL80211_IFTYPE_OCB)
497
				return -1;
498 499 500
		break;
	}

501 502
	skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
	tmp.h_proto = payload.proto;
503

504 505 506 507
	if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
		    tmp.h_proto != htons(ETH_P_AARP) &&
		    tmp.h_proto != htons(ETH_P_IPX)) ||
		   ether_addr_equal(payload.hdr, bridge_tunnel_header)))
508 509
		/* remove RFC1042 or Bridge-Tunnel encapsulation and
		 * replace EtherType */
510 511
		hdrlen += ETH_ALEN + 2;
	else
512
		tmp.h_proto = htons(skb->len - hdrlen);
513 514

	pskb_pull(skb, hdrlen);
515

516
	if (!ehdr)
517
		ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
518 519
	memcpy(ehdr, &tmp, sizeof(tmp));

520 521
	return 0;
}
522 523 524 525 526 527

int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
			   enum nl80211_iftype iftype)
{
	return __ieee80211_data_to_8023(skb, NULL, addr, iftype);
}
528 529
EXPORT_SYMBOL(ieee80211_data_to_8023);

Z
Zhu Yi 已提交
530
int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
531 532
			     enum nl80211_iftype iftype,
			     const u8 *bssid, bool qos)
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
{
	struct ieee80211_hdr hdr;
	u16 hdrlen, ethertype;
	__le16 fc;
	const u8 *encaps_data;
	int encaps_len, skip_header_bytes;
	int nh_pos, h_pos;
	int head_need;

	if (unlikely(skb->len < ETH_HLEN))
		return -EINVAL;

	nh_pos = skb_network_header(skb) - skb->data;
	h_pos = skb_transport_header(skb) - skb->data;

	/* convert Ethernet header to proper 802.11 header (based on
	 * operation mode) */
	ethertype = (skb->data[12] << 8) | skb->data[13];
	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);

	switch (iftype) {
	case NL80211_IFTYPE_AP:
	case NL80211_IFTYPE_AP_VLAN:
556
	case NL80211_IFTYPE_P2P_GO:
557 558 559 560 561 562 563 564
		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
		/* DA BSSID SA */
		memcpy(hdr.addr1, skb->data, ETH_ALEN);
		memcpy(hdr.addr2, addr, ETH_ALEN);
		memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
		hdrlen = 24;
		break;
	case NL80211_IFTYPE_STATION:
565
	case NL80211_IFTYPE_P2P_CLIENT:
566 567 568 569 570 571 572
		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
		/* BSSID SA DA */
		memcpy(hdr.addr1, bssid, ETH_ALEN);
		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
		memcpy(hdr.addr3, skb->data, ETH_ALEN);
		hdrlen = 24;
		break;
573
	case NL80211_IFTYPE_OCB:
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	case NL80211_IFTYPE_ADHOC:
		/* DA SA BSSID */
		memcpy(hdr.addr1, skb->data, ETH_ALEN);
		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
		memcpy(hdr.addr3, bssid, ETH_ALEN);
		hdrlen = 24;
		break;
	default:
		return -EOPNOTSUPP;
	}

	if (qos) {
		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
		hdrlen += 2;
	}

	hdr.frame_control = fc;
	hdr.duration_id = 0;
	hdr.seq_ctrl = 0;

	skip_header_bytes = ETH_HLEN;
	if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
		encaps_data = bridge_tunnel_header;
		encaps_len = sizeof(bridge_tunnel_header);
		skip_header_bytes -= 2;
S
Simon Horman 已提交
599
	} else if (ethertype >= ETH_P_802_3_MIN) {
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		encaps_data = rfc1042_header;
		encaps_len = sizeof(rfc1042_header);
		skip_header_bytes -= 2;
	} else {
		encaps_data = NULL;
		encaps_len = 0;
	}

	skb_pull(skb, skip_header_bytes);
	nh_pos -= skip_header_bytes;
	h_pos -= skip_header_bytes;

	head_need = hdrlen + encaps_len - skb_headroom(skb);

	if (head_need > 0 || skb_cloned(skb)) {
		head_need = max(head_need, 0);
		if (head_need)
			skb_orphan(skb);

619
		if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
620
			return -ENOMEM;
621

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
		skb->truesize += head_need;
	}

	if (encaps_data) {
		memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
		nh_pos += encaps_len;
		h_pos += encaps_len;
	}

	memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);

	nh_pos += hdrlen;
	h_pos += hdrlen;

	/* Update skb pointers to various headers since this modified frame
	 * is going to go through Linux networking code that may potentially
	 * need things like pointer to IP header. */
639
	skb_reset_mac_header(skb);
640 641 642 643 644 645 646
	skb_set_network_header(skb, nh_pos);
	skb_set_transport_header(skb, h_pos);

	return 0;
}
EXPORT_SYMBOL(ieee80211_data_from_8023);

647 648 649 650 651 652 653
static void
__frame_add_frag(struct sk_buff *skb, struct page *page,
		 void *ptr, int len, int size)
{
	struct skb_shared_info *sh = skb_shinfo(skb);
	int page_offset;

654
	page_ref_inc(page);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	page_offset = ptr - page_address(page);
	skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
}

static void
__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
			    int offset, int len)
{
	struct skb_shared_info *sh = skb_shinfo(skb);
	const skb_frag_t *frag = &sh->frags[-1];
	struct page *frag_page;
	void *frag_ptr;
	int frag_len, frag_size;
	int head_size = skb->len - skb->data_len;
	int cur_len;

	frag_page = virt_to_head_page(skb->head);
	frag_ptr = skb->data;
	frag_size = head_size;

	while (offset >= frag_size) {
		offset -= frag_size;
		frag++;
		frag_page = skb_frag_page(frag);
		frag_ptr = skb_frag_address(frag);
		frag_size = skb_frag_size(frag);
	}

	frag_ptr += offset;
	frag_len = frag_size - offset;

	cur_len = min(len, frag_len);

	__frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
	len -= cur_len;

	while (len > 0) {
		frag++;
		frag_len = skb_frag_size(frag);
		cur_len = min(len, frag_len);
		__frame_add_frag(frame, skb_frag_page(frag),
				 skb_frag_address(frag), cur_len, frag_len);
		len -= cur_len;
	}
}

701 702
static struct sk_buff *
__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
703
		       int offset, int len, bool reuse_frag)
704 705
{
	struct sk_buff *frame;
706
	int cur_len = len;
707 708 709 710

	if (skb->len - offset < len)
		return NULL;

711 712 713 714 715 716 717 718
	/*
	 * When reusing framents, copy some data to the head to simplify
	 * ethernet header handling and speed up protocol header processing
	 * in the stack later.
	 */
	if (reuse_frag)
		cur_len = min_t(int, len, 32);

719 720 721 722
	/*
	 * Allocate and reserve two bytes more for payload
	 * alignment since sizeof(struct ethhdr) is 14.
	 */
723
	frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
724 725
	if (!frame)
		return NULL;
726 727

	skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
728 729 730 731 732 733 734 735
	skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);

	len -= cur_len;
	if (!len)
		return frame;

	offset += cur_len;
	__ieee80211_amsdu_copy_frag(skb, frame, offset, len);
736 737 738

	return frame;
}
Z
Zhu Yi 已提交
739 740 741

void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
			      const u8 *addr, enum nl80211_iftype iftype,
742 743
			      const unsigned int extra_headroom,
			      bool has_80211_header)
Z
Zhu Yi 已提交
744
{
745
	unsigned int hlen = ALIGN(extra_headroom, 4);
Z
Zhu Yi 已提交
746 747 748
	struct sk_buff *frame = NULL;
	u16 ethertype;
	u8 *payload;
749 750
	int offset = 0, remaining, err;
	struct ethhdr eth;
751
	bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
752
	bool reuse_skb = false;
753
	bool last = false;
754

755
	if (has_80211_header) {
756
		err = __ieee80211_data_to_8023(skb, &eth, addr, iftype);
757 758 759
		if (err)
			goto out;
	}
Z
Zhu Yi 已提交
760

761 762 763
	while (!last) {
		unsigned int subframe_len;
		int len;
Z
Zhu Yi 已提交
764 765
		u8 padding;

766 767 768
		skb_copy_bits(skb, offset, &eth, sizeof(eth));
		len = ntohs(eth.h_proto);
		subframe_len = sizeof(struct ethhdr) + len;
Z
Zhu Yi 已提交
769
		padding = (4 - subframe_len) & 0x3;
770

Z
Zhu Yi 已提交
771
		/* the last MSDU has no padding */
772
		remaining = skb->len - offset;
Z
Zhu Yi 已提交
773 774 775
		if (subframe_len > remaining)
			goto purge;

776
		offset += sizeof(struct ethhdr);
Z
Zhu Yi 已提交
777
		/* reuse skb for the last subframe */
778
		last = remaining <= subframe_len + padding;
779
		if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
780
			skb_pull(skb, offset);
Z
Zhu Yi 已提交
781
			frame = skb;
782 783
			reuse_skb = true;
		} else {
784 785
			frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
						       reuse_frag);
Z
Zhu Yi 已提交
786 787 788
			if (!frame)
				goto purge;

789
			offset += len + padding;
Z
Zhu Yi 已提交
790 791 792 793 794 795 796 797
		}

		skb_reset_network_header(frame);
		frame->dev = skb->dev;
		frame->priority = skb->priority;

		payload = frame->data;
		ethertype = (payload[6] << 8) | payload[7];
798
		if (likely((ether_addr_equal(payload, rfc1042_header) &&
Z
Zhu Yi 已提交
799
			    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
800
			   ether_addr_equal(payload, bridge_tunnel_header))) {
801 802
			eth.h_proto = htons(ethertype);
			skb_pull(frame, ETH_ALEN + 2);
Z
Zhu Yi 已提交
803
		}
804 805

		memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
Z
Zhu Yi 已提交
806 807 808
		__skb_queue_tail(list, frame);
	}

809 810 811
	if (!reuse_skb)
		dev_kfree_skb(skb);

Z
Zhu Yi 已提交
812 813 814 815 816 817 818 819 820
	return;

 purge:
	__skb_queue_purge(list);
 out:
	dev_kfree_skb(skb);
}
EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);

821
/* Given a data frame determine the 802.1p/1d tag to use. */
822 823
unsigned int cfg80211_classify8021d(struct sk_buff *skb,
				    struct cfg80211_qos_map *qos_map)
824 825
{
	unsigned int dscp;
826
	unsigned char vlan_priority;
827 828 829 830 831 832 833 834 835

	/* skb->priority values from 256->263 are magic values to
	 * directly indicate a specific 802.1d priority.  This is used
	 * to allow 802.1d priority to be passed directly in from VLAN
	 * tags, etc.
	 */
	if (skb->priority >= 256 && skb->priority <= 263)
		return skb->priority - 256;

836 837
	if (skb_vlan_tag_present(skb)) {
		vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
838 839 840 841 842
			>> VLAN_PRIO_SHIFT;
		if (vlan_priority > 0)
			return vlan_priority;
	}

843 844
	switch (skb->protocol) {
	case htons(ETH_P_IP):
845 846 847 848
		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
		break;
	case htons(ETH_P_IPV6):
		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
849
		break;
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
	case htons(ETH_P_MPLS_UC):
	case htons(ETH_P_MPLS_MC): {
		struct mpls_label mpls_tmp, *mpls;

		mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
					  sizeof(*mpls), &mpls_tmp);
		if (!mpls)
			return 0;

		return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
			>> MPLS_LS_TC_SHIFT;
	}
	case htons(ETH_P_80221):
		/* 802.21 is always network control traffic */
		return 7;
865 866 867 868
	default:
		return 0;
	}

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
	if (qos_map) {
		unsigned int i, tmp_dscp = dscp >> 2;

		for (i = 0; i < qos_map->num_des; i++) {
			if (tmp_dscp == qos_map->dscp_exception[i].dscp)
				return qos_map->dscp_exception[i].up;
		}

		for (i = 0; i < 8; i++) {
			if (tmp_dscp >= qos_map->up[i].low &&
			    tmp_dscp <= qos_map->up[i].high)
				return i;
		}
	}

884 885 886
	return dscp >> 5;
}
EXPORT_SYMBOL(cfg80211_classify8021d);
887 888 889

const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
{
890 891 892 893
	const struct cfg80211_bss_ies *ies;

	ies = rcu_dereference(bss->ies);
	if (!ies)
894
		return NULL;
895 896

	return cfg80211_find_ie(ie, ies->data, ies->len);
897 898
}
EXPORT_SYMBOL(ieee80211_bss_get_ie);
J
Johannes Berg 已提交
899 900 901

void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
{
902
	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
J
Johannes Berg 已提交
903 904 905 906 907 908 909 910 911
	struct net_device *dev = wdev->netdev;
	int i;

	if (!wdev->connect_keys)
		return;

	for (i = 0; i < 6; i++) {
		if (!wdev->connect_keys->params[i].cipher)
			continue;
912 913
		if (rdev_add_key(rdev, dev, i, false, NULL,
				 &wdev->connect_keys->params[i])) {
914
			netdev_err(dev, "failed to set key %d\n", i);
915 916
			continue;
		}
J
Johannes Berg 已提交
917
		if (wdev->connect_keys->def == i)
918
			if (rdev_set_default_key(rdev, dev, i, true, true)) {
919
				netdev_err(dev, "failed to set defkey %d\n", i);
920 921
				continue;
			}
J
Johannes Berg 已提交
922
		if (wdev->connect_keys->defmgmt == i)
923
			if (rdev_set_default_mgmt_key(rdev, dev, i))
924
				netdev_err(dev, "failed to set mgtdef %d\n", i);
J
Johannes Berg 已提交
925 926
	}

927
	kzfree(wdev->connect_keys);
J
Johannes Berg 已提交
928 929
	wdev->connect_keys = NULL;
}
930

931
void cfg80211_process_wdev_events(struct wireless_dev *wdev)
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
{
	struct cfg80211_event *ev;
	unsigned long flags;
	const u8 *bssid = NULL;

	spin_lock_irqsave(&wdev->event_lock, flags);
	while (!list_empty(&wdev->event_list)) {
		ev = list_first_entry(&wdev->event_list,
				      struct cfg80211_event, list);
		list_del(&ev->list);
		spin_unlock_irqrestore(&wdev->event_lock, flags);

		wdev_lock(wdev);
		switch (ev->type) {
		case EVENT_CONNECT_RESULT:
			if (!is_zero_ether_addr(ev->cr.bssid))
				bssid = ev->cr.bssid;
			__cfg80211_connect_result(
				wdev->netdev, bssid,
				ev->cr.req_ie, ev->cr.req_ie_len,
				ev->cr.resp_ie, ev->cr.resp_ie_len,
				ev->cr.status,
				ev->cr.status == WLAN_STATUS_SUCCESS,
955
				ev->cr.bss);
956 957
			break;
		case EVENT_ROAMED:
958 959 960
			__cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
					  ev->rm.req_ie_len, ev->rm.resp_ie,
					  ev->rm.resp_ie_len);
961 962 963 964
			break;
		case EVENT_DISCONNECTED:
			__cfg80211_disconnected(wdev->netdev,
						ev->dc.ie, ev->dc.ie_len,
965 966
						ev->dc.reason,
						!ev->dc.locally_generated);
967 968
			break;
		case EVENT_IBSS_JOINED:
969 970
			__cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
					       ev->ij.channel);
971
			break;
972 973 974
		case EVENT_STOPPED:
			__cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
			break;
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
		}
		wdev_unlock(wdev);

		kfree(ev);

		spin_lock_irqsave(&wdev->event_lock, flags);
	}
	spin_unlock_irqrestore(&wdev->event_lock, flags);
}

void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
{
	struct wireless_dev *wdev;

	ASSERT_RTNL();

991
	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
992 993 994 995 996 997 998 999 1000 1001
		cfg80211_process_wdev_events(wdev);
}

int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
			  struct net_device *dev, enum nl80211_iftype ntype,
			  u32 *flags, struct vif_params *params)
{
	int err;
	enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;

1002
	ASSERT_RTNL();
1003 1004 1005 1006 1007

	/* don't support changing VLANs, you just re-create them */
	if (otype == NL80211_IFTYPE_AP_VLAN)
		return -EOPNOTSUPP;

1008 1009 1010 1011
	/* cannot change into P2P device type */
	if (ntype == NL80211_IFTYPE_P2P_DEVICE)
		return -EOPNOTSUPP;

1012 1013 1014 1015
	if (!rdev->ops->change_virtual_intf ||
	    !(rdev->wiphy.interface_modes & (1 << ntype)))
		return -EOPNOTSUPP;

1016
	/* if it's part of a bridge, reject changing type to station/ibss */
1017
	if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
1018 1019 1020
	    (ntype == NL80211_IFTYPE_ADHOC ||
	     ntype == NL80211_IFTYPE_STATION ||
	     ntype == NL80211_IFTYPE_P2P_CLIENT))
1021 1022
		return -EBUSY;

1023
	if (ntype != otype) {
1024
		dev->ieee80211_ptr->use_4addr = false;
1025
		dev->ieee80211_ptr->mesh_id_up_len = 0;
1026
		wdev_lock(dev->ieee80211_ptr);
1027
		rdev_set_qos_map(rdev, dev, NULL);
1028
		wdev_unlock(dev->ieee80211_ptr);
1029

1030
		switch (otype) {
1031
		case NL80211_IFTYPE_AP:
1032
			cfg80211_stop_ap(rdev, dev, true);
1033
			break;
1034 1035 1036 1037
		case NL80211_IFTYPE_ADHOC:
			cfg80211_leave_ibss(rdev, dev, false);
			break;
		case NL80211_IFTYPE_STATION:
1038
		case NL80211_IFTYPE_P2P_CLIENT:
1039
			wdev_lock(dev->ieee80211_ptr);
1040 1041
			cfg80211_disconnect(rdev, dev,
					    WLAN_REASON_DEAUTH_LEAVING, true);
1042
			wdev_unlock(dev->ieee80211_ptr);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
			break;
		case NL80211_IFTYPE_MESH_POINT:
			/* mesh should be handled? */
			break;
		default:
			break;
		}

		cfg80211_process_rdev_events(rdev);
	}

1054
	err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
1055 1056 1057

	WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);

1058 1059 1060
	if (!err && params && params->use_4addr != -1)
		dev->ieee80211_ptr->use_4addr = params->use_4addr;

1061 1062 1063 1064 1065 1066 1067
	if (!err) {
		dev->priv_flags &= ~IFF_DONT_BRIDGE;
		switch (ntype) {
		case NL80211_IFTYPE_STATION:
			if (dev->ieee80211_ptr->use_4addr)
				break;
			/* fall through */
1068
		case NL80211_IFTYPE_OCB:
1069
		case NL80211_IFTYPE_P2P_CLIENT:
1070 1071 1072
		case NL80211_IFTYPE_ADHOC:
			dev->priv_flags |= IFF_DONT_BRIDGE;
			break;
1073
		case NL80211_IFTYPE_P2P_GO:
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
		case NL80211_IFTYPE_AP:
		case NL80211_IFTYPE_AP_VLAN:
		case NL80211_IFTYPE_WDS:
		case NL80211_IFTYPE_MESH_POINT:
			/* bridging OK */
			break;
		case NL80211_IFTYPE_MONITOR:
			/* monitor can't bridge anyway */
			break;
		case NL80211_IFTYPE_UNSPECIFIED:
1084
		case NUM_NL80211_IFTYPES:
1085 1086
			/* not happening */
			break;
1087 1088 1089
		case NL80211_IFTYPE_P2P_DEVICE:
			WARN_ON(1);
			break;
1090 1091 1092
		}
	}

1093 1094 1095 1096 1097
	if (!err && ntype != otype && netif_running(dev)) {
		cfg80211_update_iface_num(rdev, ntype, 1);
		cfg80211_update_iface_num(rdev, otype, -1);
	}

1098 1099
	return err;
}
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
{
	static const u32 __mcs2bitrate[] = {
		/* control PHY */
		[0] =   275,
		/* SC PHY */
		[1] =  3850,
		[2] =  7700,
		[3] =  9625,
		[4] = 11550,
		[5] = 12512, /* 1251.25 mbps */
		[6] = 15400,
		[7] = 19250,
		[8] = 23100,
		[9] = 25025,
		[10] = 30800,
		[11] = 38500,
		[12] = 46200,
		/* OFDM PHY */
		[13] =  6930,
		[14] =  8662, /* 866.25 mbps */
		[15] = 13860,
		[16] = 17325,
		[17] = 20790,
		[18] = 27720,
		[19] = 34650,
		[20] = 41580,
		[21] = 45045,
		[22] = 51975,
		[23] = 62370,
		[24] = 67568, /* 6756.75 mbps */
		/* LP-SC PHY */
		[25] =  6260,
		[26] =  8340,
		[27] = 11120,
		[28] = 12510,
		[29] = 16680,
		[30] = 22240,
		[31] = 25030,
	};

	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
		return 0;

	return __mcs2bitrate[rate->mcs];
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
{
	static const u32 base[4][10] = {
		{   6500000,
		   13000000,
		   19500000,
		   26000000,
		   39000000,
		   52000000,
		   58500000,
		   65000000,
		   78000000,
		   0,
		},
		{  13500000,
		   27000000,
		   40500000,
		   54000000,
		   81000000,
		  108000000,
		  121500000,
		  135000000,
		  162000000,
		  180000000,
		},
		{  29300000,
		   58500000,
		   87800000,
		  117000000,
		  175500000,
		  234000000,
		  263300000,
		  292500000,
		  351000000,
		  390000000,
		},
		{  58500000,
		  117000000,
		  175500000,
		  234000000,
		  351000000,
		  468000000,
		  526500000,
		  585000000,
		  702000000,
		  780000000,
		},
	};
	u32 bitrate;
	int idx;

	if (WARN_ON_ONCE(rate->mcs > 9))
		return 0;

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	switch (rate->bw) {
	case RATE_INFO_BW_160:
		idx = 3;
		break;
	case RATE_INFO_BW_80:
		idx = 2;
		break;
	case RATE_INFO_BW_40:
		idx = 1;
		break;
	case RATE_INFO_BW_5:
	case RATE_INFO_BW_10:
	default:
		WARN_ON(1);
		/* fall through */
	case RATE_INFO_BW_20:
		idx = 0;
	}
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

	bitrate = base[idx][rate->mcs];
	bitrate *= rate->nss;

	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
		bitrate = (bitrate / 9) * 10;

	/* do NOT round down here */
	return (bitrate + 50000) / 100000;
}

1231
u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1232 1233 1234
{
	int modulation, streams, bitrate;

1235 1236
	if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
	    !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
1237
		return rate->legacy;
1238 1239
	if (rate->flags & RATE_INFO_FLAGS_60G)
		return cfg80211_calculate_bitrate_60g(rate);
1240 1241
	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
		return cfg80211_calculate_bitrate_vht(rate);
1242 1243

	/* the formula below does only work for MCS values smaller than 32 */
1244
	if (WARN_ON_ONCE(rate->mcs >= 32))
1245 1246 1247 1248 1249
		return 0;

	modulation = rate->mcs & 7;
	streams = (rate->mcs >> 3) + 1;

1250
	bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

	if (modulation < 4)
		bitrate *= (modulation + 1);
	else if (modulation == 4)
		bitrate *= (modulation + 2);
	else
		bitrate *= (modulation + 3);

	bitrate *= streams;

	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
		bitrate = (bitrate / 9) * 10;

	/* do NOT round down here */
	return (bitrate + 50000) / 100000;
}
1267
EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1268

1269 1270 1271
int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
			  enum ieee80211_p2p_attr_id attr,
			  u8 *buf, unsigned int bufsize)
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
{
	u8 *out = buf;
	u16 attr_remaining = 0;
	bool desired_attr = false;
	u16 desired_len = 0;

	while (len > 0) {
		unsigned int iedatalen;
		unsigned int copy;
		const u8 *iedata;

		if (len < 2)
			return -EILSEQ;
		iedatalen = ies[1];
		if (iedatalen + 2 > len)
			return -EILSEQ;

		if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
			goto cont;

		if (iedatalen < 4)
			goto cont;

		iedata = ies + 2;

		/* check WFA OUI, P2P subtype */
		if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
		    iedata[2] != 0x9a || iedata[3] != 0x09)
			goto cont;

		iedatalen -= 4;
		iedata += 4;

		/* check attribute continuation into this IE */
		copy = min_t(unsigned int, attr_remaining, iedatalen);
		if (copy && desired_attr) {
			desired_len += copy;
			if (out) {
				memcpy(out, iedata, min(bufsize, copy));
				out += min(bufsize, copy);
				bufsize -= min(bufsize, copy);
			}


			if (copy == attr_remaining)
				return desired_len;
		}

		attr_remaining -= copy;
		if (attr_remaining)
			goto cont;

		iedatalen -= copy;
		iedata += copy;

		while (iedatalen > 0) {
			u16 attr_len;

			/* P2P attribute ID & size must fit */
			if (iedatalen < 3)
				return -EILSEQ;
			desired_attr = iedata[0] == attr;
			attr_len = get_unaligned_le16(iedata + 1);
			iedatalen -= 3;
			iedata += 3;

			copy = min_t(unsigned int, attr_len, iedatalen);

			if (desired_attr) {
				desired_len += copy;
				if (out) {
					memcpy(out, iedata, min(bufsize, copy));
					out += min(bufsize, copy);
					bufsize -= min(bufsize, copy);
				}

				if (copy == attr_len)
					return desired_len;
			}

			iedata += copy;
			iedatalen -= copy;
			attr_remaining = attr_len - copy;
		}

 cont:
		len -= ies[1] + 2;
		ies += ies[1] + 2;
	}

	if (attr_remaining && desired_attr)
		return -EILSEQ;

	return -ENOENT;
}
EXPORT_SYMBOL(cfg80211_get_p2p_attr);

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
{
	int i;

	for (i = 0; i < n_ids; i++)
		if (ids[i] == id)
			return true;
	return false;
}

size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
			      const u8 *ids, int n_ids,
			      const u8 *after_ric, int n_after_ric,
			      size_t offset)
{
	size_t pos = offset;

	while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
		if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
			pos += 2 + ies[pos + 1];

			while (pos < ielen &&
			       !ieee80211_id_in_list(after_ric, n_after_ric,
						     ies[pos]))
				pos += 2 + ies[pos + 1];
		} else {
			pos += 2 + ies[pos + 1];
		}
	}

	return pos;
}
EXPORT_SYMBOL(ieee80211_ie_split_ric);

1403
bool ieee80211_operating_class_to_band(u8 operating_class,
1404
				       enum nl80211_band *band)
1405 1406 1407 1408
{
	switch (operating_class) {
	case 112:
	case 115 ... 127:
1409
	case 128 ... 130:
1410
		*band = NL80211_BAND_5GHZ;
1411 1412 1413 1414 1415
		return true;
	case 81:
	case 82:
	case 83:
	case 84:
1416
		*band = NL80211_BAND_2GHZ;
1417
		return true;
1418
	case 180:
1419
		*band = NL80211_BAND_60GHZ;
1420
		return true;
1421 1422 1423 1424 1425 1426
	}

	return false;
}
EXPORT_SYMBOL(ieee80211_operating_class_to_band);

1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
					  u8 *op_class)
{
	u8 vht_opclass;
	u16 freq = chandef->center_freq1;

	if (freq >= 2412 && freq <= 2472) {
		if (chandef->width > NL80211_CHAN_WIDTH_40)
			return false;

		/* 2.407 GHz, channels 1..13 */
		if (chandef->width == NL80211_CHAN_WIDTH_40) {
			if (freq > chandef->chan->center_freq)
				*op_class = 83; /* HT40+ */
			else
				*op_class = 84; /* HT40- */
		} else {
			*op_class = 81;
		}

		return true;
	}

	if (freq == 2484) {
		if (chandef->width > NL80211_CHAN_WIDTH_40)
			return false;

		*op_class = 82; /* channel 14 */
		return true;
	}

	switch (chandef->width) {
	case NL80211_CHAN_WIDTH_80:
		vht_opclass = 128;
		break;
	case NL80211_CHAN_WIDTH_160:
		vht_opclass = 129;
		break;
	case NL80211_CHAN_WIDTH_80P80:
		vht_opclass = 130;
		break;
	case NL80211_CHAN_WIDTH_10:
	case NL80211_CHAN_WIDTH_5:
		return false; /* unsupported for now */
	default:
		vht_opclass = 0;
		break;
	}

	/* 5 GHz, channels 36..48 */
	if (freq >= 5180 && freq <= 5240) {
		if (vht_opclass) {
			*op_class = vht_opclass;
		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
			if (freq > chandef->chan->center_freq)
				*op_class = 116;
			else
				*op_class = 117;
		} else {
			*op_class = 115;
		}

		return true;
	}

	/* 5 GHz, channels 52..64 */
	if (freq >= 5260 && freq <= 5320) {
		if (vht_opclass) {
			*op_class = vht_opclass;
		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
			if (freq > chandef->chan->center_freq)
				*op_class = 119;
			else
				*op_class = 120;
		} else {
			*op_class = 118;
		}

		return true;
	}

	/* 5 GHz, channels 100..144 */
	if (freq >= 5500 && freq <= 5720) {
		if (vht_opclass) {
			*op_class = vht_opclass;
		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
			if (freq > chandef->chan->center_freq)
				*op_class = 122;
			else
				*op_class = 123;
		} else {
			*op_class = 121;
		}

		return true;
	}

	/* 5 GHz, channels 149..169 */
	if (freq >= 5745 && freq <= 5845) {
		if (vht_opclass) {
			*op_class = vht_opclass;
		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
			if (freq > chandef->chan->center_freq)
				*op_class = 126;
			else
				*op_class = 127;
		} else if (freq <= 5805) {
			*op_class = 124;
		} else {
			*op_class = 125;
		}

		return true;
	}

	/* 56.16 GHz, channel 1..4 */
	if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
		if (chandef->width >= NL80211_CHAN_WIDTH_40)
			return false;

		*op_class = 180;
		return true;
	}

	/* not supported yet */
	return false;
}
EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);

1556 1557 1558 1559 1560 1561
int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
				 u32 beacon_int)
{
	struct wireless_dev *wdev;
	int res = 0;

1562
	if (beacon_int < 10 || beacon_int > 10000)
1563 1564
		return -EINVAL;

1565
	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
		if (!wdev->beacon_interval)
			continue;
		if (wdev->beacon_interval != beacon_int) {
			res = -EINVAL;
			break;
		}
	}

	return res;
}
1576

1577 1578 1579 1580 1581 1582 1583
int cfg80211_iter_combinations(struct wiphy *wiphy,
			       const int num_different_channels,
			       const u8 radar_detect,
			       const int iftype_num[NUM_NL80211_IFTYPES],
			       void (*iter)(const struct ieee80211_iface_combination *c,
					    void *data),
			       void *data)
1584
{
1585 1586
	const struct ieee80211_regdomain *regdom;
	enum nl80211_dfs_regions region = 0;
1587 1588 1589 1590
	int i, j, iftype;
	int num_interfaces = 0;
	u32 used_iftypes = 0;

1591 1592 1593 1594 1595 1596 1597 1598
	if (radar_detect) {
		rcu_read_lock();
		regdom = rcu_dereference(cfg80211_regdomain);
		if (regdom)
			region = regdom->dfs_region;
		rcu_read_unlock();
	}

1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
		num_interfaces += iftype_num[iftype];
		if (iftype_num[iftype] > 0 &&
		    !(wiphy->software_iftypes & BIT(iftype)))
			used_iftypes |= BIT(iftype);
	}

	for (i = 0; i < wiphy->n_iface_combinations; i++) {
		const struct ieee80211_iface_combination *c;
		struct ieee80211_iface_limit *limits;
		u32 all_iftypes = 0;

		c = &wiphy->iface_combinations[i];

		if (num_interfaces > c->max_interfaces)
			continue;
		if (num_different_channels > c->num_different_channels)
			continue;

		limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
				 GFP_KERNEL);
		if (!limits)
			return -ENOMEM;

		for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
			if (wiphy->software_iftypes & BIT(iftype))
				continue;
			for (j = 0; j < c->n_limits; j++) {
				all_iftypes |= limits[j].types;
				if (!(limits[j].types & BIT(iftype)))
					continue;
				if (limits[j].max < iftype_num[iftype])
					goto cont;
				limits[j].max -= iftype_num[iftype];
			}
		}

1636
		if (radar_detect != (c->radar_detect_widths & radar_detect))
1637 1638
			goto cont;

1639 1640 1641 1642
		if (radar_detect && c->radar_detect_regions &&
		    !(c->radar_detect_regions & BIT(region)))
			goto cont;

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
		/* Finally check that all iftypes that we're currently
		 * using are actually part of this combination. If they
		 * aren't then we can't use this combination and have
		 * to continue to the next.
		 */
		if ((all_iftypes & used_iftypes) != used_iftypes)
			goto cont;

		/* This combination covered all interface types and
		 * supported the requested numbers, so we're good.
		 */
1654 1655

		(*iter)(c, data);
1656 1657 1658 1659
 cont:
		kfree(limits);
	}

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
	return 0;
}
EXPORT_SYMBOL(cfg80211_iter_combinations);

static void
cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
			  void *data)
{
	int *num = data;
	(*num)++;
}

int cfg80211_check_combinations(struct wiphy *wiphy,
				const int num_different_channels,
				const u8 radar_detect,
				const int iftype_num[NUM_NL80211_IFTYPES])
{
	int err, num = 0;

	err = cfg80211_iter_combinations(wiphy, num_different_channels,
					 radar_detect, iftype_num,
					 cfg80211_iter_sum_ifcombs, &num);
	if (err)
		return err;
	if (num == 0)
		return -EBUSY;

	return 0;
1688 1689 1690
}
EXPORT_SYMBOL(cfg80211_check_combinations);

1691 1692 1693 1694 1695 1696
int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
			   const u8 *rates, unsigned int n_rates,
			   u32 *mask)
{
	int i, j;

1697 1698 1699
	if (!sband)
		return -EINVAL;

1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
	if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
		return -EINVAL;

	*mask = 0;

	for (i = 0; i < n_rates; i++) {
		int rate = (rates[i] & 0x7f) * 5;
		bool found = false;

		for (j = 0; j < sband->n_bitrates; j++) {
			if (sband->bitrates[j].bitrate == rate) {
				found = true;
				*mask |= BIT(j);
				break;
			}
		}
		if (!found)
			return -EINVAL;
	}

	/*
	 * mask must have at least one bit set here since we
	 * didn't accept a 0-length rates array nor allowed
	 * entries in the array that didn't exist
	 */

	return 0;
}
1728

1729 1730
unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
{
1731
	enum nl80211_band band;
1732 1733
	unsigned int n_channels = 0;

1734
	for (band = 0; band < NUM_NL80211_BANDS; band++)
1735 1736 1737 1738 1739 1740 1741
		if (wiphy->bands[band])
			n_channels += wiphy->bands[band]->n_channels;

	return n_channels;
}
EXPORT_SYMBOL(ieee80211_get_num_supported_channels);

1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
			 struct station_info *sinfo)
{
	struct cfg80211_registered_device *rdev;
	struct wireless_dev *wdev;

	wdev = dev->ieee80211_ptr;
	if (!wdev)
		return -EOPNOTSUPP;

	rdev = wiphy_to_rdev(wdev->wiphy);
	if (!rdev->ops->get_station)
		return -EOPNOTSUPP;

	return rdev_get_station(rdev, dev, mac_addr, sinfo);
}
EXPORT_SYMBOL(cfg80211_get_station);

1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
const unsigned char rfc1042_header[] __aligned(2) =
	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
EXPORT_SYMBOL(rfc1042_header);

/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
const unsigned char bridge_tunnel_header[] __aligned(2) =
	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
EXPORT_SYMBOL(bridge_tunnel_header);