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