Commit | Line | Data |
---|---|---|
8318d78a JB |
1 | /* |
2 | * Wireless utility functions | |
3 | * | |
d3236553 | 4 | * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net> |
8318d78a | 5 | */ |
d3236553 | 6 | #include <linux/bitops.h> |
e31a16d6 | 7 | #include <linux/etherdevice.h> |
d3236553 | 8 | #include <net/cfg80211.h> |
e31a16d6 | 9 | #include <net/ip.h> |
8318d78a JB |
10 | #include "core.h" |
11 | ||
bd815252 JB |
12 | struct ieee80211_rate * |
13 | ieee80211_get_response_rate(struct ieee80211_supported_band *sband, | |
881d948c | 14 | u32 basic_rates, int bitrate) |
bd815252 JB |
15 | { |
16 | struct ieee80211_rate *result = &sband->bitrates[0]; | |
17 | int i; | |
18 | ||
19 | for (i = 0; i < sband->n_bitrates; i++) { | |
20 | if (!(basic_rates & BIT(i))) | |
21 | continue; | |
22 | if (sband->bitrates[i].bitrate > bitrate) | |
23 | continue; | |
24 | result = &sband->bitrates[i]; | |
25 | } | |
26 | ||
27 | return result; | |
28 | } | |
29 | EXPORT_SYMBOL(ieee80211_get_response_rate); | |
30 | ||
8318d78a JB |
31 | int ieee80211_channel_to_frequency(int chan) |
32 | { | |
33 | if (chan < 14) | |
34 | return 2407 + chan * 5; | |
35 | ||
36 | if (chan == 14) | |
37 | return 2484; | |
38 | ||
39 | /* FIXME: 802.11j 17.3.8.3.2 */ | |
40 | return (chan + 1000) * 5; | |
41 | } | |
42 | EXPORT_SYMBOL(ieee80211_channel_to_frequency); | |
43 | ||
44 | int ieee80211_frequency_to_channel(int freq) | |
45 | { | |
46 | if (freq == 2484) | |
47 | return 14; | |
48 | ||
49 | if (freq < 2484) | |
50 | return (freq - 2407) / 5; | |
51 | ||
52 | /* FIXME: 802.11j 17.3.8.3.2 */ | |
53 | return freq/5 - 1000; | |
54 | } | |
55 | EXPORT_SYMBOL(ieee80211_frequency_to_channel); | |
56 | ||
6c507cd0 JB |
57 | struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy, |
58 | int freq) | |
906c730a JB |
59 | { |
60 | enum ieee80211_band band; | |
61 | struct ieee80211_supported_band *sband; | |
62 | int i; | |
63 | ||
64 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
65 | sband = wiphy->bands[band]; | |
66 | ||
67 | if (!sband) | |
68 | continue; | |
69 | ||
70 | for (i = 0; i < sband->n_channels; i++) { | |
71 | if (sband->channels[i].center_freq == freq) | |
72 | return &sband->channels[i]; | |
73 | } | |
74 | } | |
75 | ||
76 | return NULL; | |
77 | } | |
6c507cd0 | 78 | EXPORT_SYMBOL(__ieee80211_get_channel); |
906c730a | 79 | |
8318d78a JB |
80 | static void set_mandatory_flags_band(struct ieee80211_supported_band *sband, |
81 | enum ieee80211_band band) | |
82 | { | |
83 | int i, want; | |
84 | ||
85 | switch (band) { | |
86 | case IEEE80211_BAND_5GHZ: | |
87 | want = 3; | |
88 | for (i = 0; i < sband->n_bitrates; i++) { | |
89 | if (sband->bitrates[i].bitrate == 60 || | |
90 | sband->bitrates[i].bitrate == 120 || | |
91 | sband->bitrates[i].bitrate == 240) { | |
92 | sband->bitrates[i].flags |= | |
93 | IEEE80211_RATE_MANDATORY_A; | |
94 | want--; | |
95 | } | |
96 | } | |
97 | WARN_ON(want); | |
98 | break; | |
99 | case IEEE80211_BAND_2GHZ: | |
100 | want = 7; | |
101 | for (i = 0; i < sband->n_bitrates; i++) { | |
102 | if (sband->bitrates[i].bitrate == 10) { | |
103 | sband->bitrates[i].flags |= | |
104 | IEEE80211_RATE_MANDATORY_B | | |
105 | IEEE80211_RATE_MANDATORY_G; | |
106 | want--; | |
107 | } | |
108 | ||
109 | if (sband->bitrates[i].bitrate == 20 || | |
110 | sband->bitrates[i].bitrate == 55 || | |
111 | sband->bitrates[i].bitrate == 110 || | |
112 | sband->bitrates[i].bitrate == 60 || | |
113 | sband->bitrates[i].bitrate == 120 || | |
114 | sband->bitrates[i].bitrate == 240) { | |
115 | sband->bitrates[i].flags |= | |
116 | IEEE80211_RATE_MANDATORY_G; | |
117 | want--; | |
118 | } | |
119 | ||
aac09fbf JB |
120 | if (sband->bitrates[i].bitrate != 10 && |
121 | sband->bitrates[i].bitrate != 20 && | |
122 | sband->bitrates[i].bitrate != 55 && | |
123 | sband->bitrates[i].bitrate != 110) | |
8318d78a JB |
124 | sband->bitrates[i].flags |= |
125 | IEEE80211_RATE_ERP_G; | |
126 | } | |
406f2388 | 127 | WARN_ON(want != 0 && want != 3 && want != 6); |
8318d78a JB |
128 | break; |
129 | case IEEE80211_NUM_BANDS: | |
130 | WARN_ON(1); | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | void ieee80211_set_bitrate_flags(struct wiphy *wiphy) | |
136 | { | |
137 | enum ieee80211_band band; | |
138 | ||
139 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) | |
140 | if (wiphy->bands[band]) | |
141 | set_mandatory_flags_band(wiphy->bands[band], band); | |
142 | } | |
08645126 JB |
143 | |
144 | int cfg80211_validate_key_settings(struct key_params *params, int key_idx, | |
145 | const u8 *mac_addr) | |
146 | { | |
147 | if (key_idx > 5) | |
148 | return -EINVAL; | |
149 | ||
150 | /* | |
151 | * Disallow pairwise keys with non-zero index unless it's WEP | |
152 | * (because current deployments use pairwise WEP keys with | |
153 | * non-zero indizes but 802.11i clearly specifies to use zero) | |
154 | */ | |
155 | if (mac_addr && key_idx && | |
156 | params->cipher != WLAN_CIPHER_SUITE_WEP40 && | |
157 | params->cipher != WLAN_CIPHER_SUITE_WEP104) | |
158 | return -EINVAL; | |
159 | ||
160 | /* TODO: add definitions for the lengths to linux/ieee80211.h */ | |
161 | switch (params->cipher) { | |
162 | case WLAN_CIPHER_SUITE_WEP40: | |
163 | if (params->key_len != 5) | |
164 | return -EINVAL; | |
165 | break; | |
166 | case WLAN_CIPHER_SUITE_TKIP: | |
167 | if (params->key_len != 32) | |
168 | return -EINVAL; | |
169 | break; | |
170 | case WLAN_CIPHER_SUITE_CCMP: | |
171 | if (params->key_len != 16) | |
172 | return -EINVAL; | |
173 | break; | |
174 | case WLAN_CIPHER_SUITE_WEP104: | |
175 | if (params->key_len != 13) | |
176 | return -EINVAL; | |
177 | break; | |
178 | case WLAN_CIPHER_SUITE_AES_CMAC: | |
179 | if (params->key_len != 16) | |
180 | return -EINVAL; | |
181 | break; | |
182 | default: | |
183 | return -EINVAL; | |
184 | } | |
185 | ||
9f26a952 JM |
186 | if (params->seq) { |
187 | switch (params->cipher) { | |
188 | case WLAN_CIPHER_SUITE_WEP40: | |
189 | case WLAN_CIPHER_SUITE_WEP104: | |
190 | /* These ciphers do not use key sequence */ | |
191 | return -EINVAL; | |
192 | case WLAN_CIPHER_SUITE_TKIP: | |
193 | case WLAN_CIPHER_SUITE_CCMP: | |
194 | case WLAN_CIPHER_SUITE_AES_CMAC: | |
195 | if (params->seq_len != 6) | |
196 | return -EINVAL; | |
197 | break; | |
198 | } | |
199 | } | |
200 | ||
08645126 JB |
201 | return 0; |
202 | } | |
e31a16d6 ZY |
203 | |
204 | /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */ | |
205 | /* Ethernet-II snap header (RFC1042 for most EtherTypes) */ | |
206 | const unsigned char rfc1042_header[] __aligned(2) = | |
207 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; | |
208 | EXPORT_SYMBOL(rfc1042_header); | |
209 | ||
210 | /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */ | |
211 | const unsigned char bridge_tunnel_header[] __aligned(2) = | |
212 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 }; | |
213 | EXPORT_SYMBOL(bridge_tunnel_header); | |
214 | ||
215 | unsigned int ieee80211_hdrlen(__le16 fc) | |
216 | { | |
217 | unsigned int hdrlen = 24; | |
218 | ||
219 | if (ieee80211_is_data(fc)) { | |
220 | if (ieee80211_has_a4(fc)) | |
221 | hdrlen = 30; | |
222 | if (ieee80211_is_data_qos(fc)) | |
223 | hdrlen += IEEE80211_QOS_CTL_LEN; | |
224 | goto out; | |
225 | } | |
226 | ||
227 | if (ieee80211_is_ctl(fc)) { | |
228 | /* | |
229 | * ACK and CTS are 10 bytes, all others 16. To see how | |
230 | * to get this condition consider | |
231 | * subtype mask: 0b0000000011110000 (0x00F0) | |
232 | * ACK subtype: 0b0000000011010000 (0x00D0) | |
233 | * CTS subtype: 0b0000000011000000 (0x00C0) | |
234 | * bits that matter: ^^^ (0x00E0) | |
235 | * value of those: 0b0000000011000000 (0x00C0) | |
236 | */ | |
237 | if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0)) | |
238 | hdrlen = 10; | |
239 | else | |
240 | hdrlen = 16; | |
241 | } | |
242 | out: | |
243 | return hdrlen; | |
244 | } | |
245 | EXPORT_SYMBOL(ieee80211_hdrlen); | |
246 | ||
247 | unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb) | |
248 | { | |
249 | const struct ieee80211_hdr *hdr = | |
250 | (const struct ieee80211_hdr *)skb->data; | |
251 | unsigned int hdrlen; | |
252 | ||
253 | if (unlikely(skb->len < 10)) | |
254 | return 0; | |
255 | hdrlen = ieee80211_hdrlen(hdr->frame_control); | |
256 | if (unlikely(hdrlen > skb->len)) | |
257 | return 0; | |
258 | return hdrlen; | |
259 | } | |
260 | EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb); | |
261 | ||
262 | int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr) | |
263 | { | |
264 | int ae = meshhdr->flags & MESH_FLAGS_AE; | |
265 | /* 7.1.3.5a.2 */ | |
266 | switch (ae) { | |
267 | case 0: | |
268 | return 6; | |
269 | case 1: | |
270 | return 12; | |
271 | case 2: | |
272 | return 18; | |
273 | case 3: | |
274 | return 24; | |
275 | default: | |
276 | return 6; | |
277 | } | |
278 | } | |
279 | ||
280 | int ieee80211_data_to_8023(struct sk_buff *skb, u8 *addr, | |
281 | enum nl80211_iftype iftype) | |
282 | { | |
283 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; | |
284 | u16 hdrlen, ethertype; | |
285 | u8 *payload; | |
286 | u8 dst[ETH_ALEN]; | |
287 | u8 src[ETH_ALEN] __aligned(2); | |
288 | ||
289 | if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) | |
290 | return -1; | |
291 | ||
292 | hdrlen = ieee80211_hdrlen(hdr->frame_control); | |
293 | ||
294 | /* convert IEEE 802.11 header + possible LLC headers into Ethernet | |
295 | * header | |
296 | * IEEE 802.11 address fields: | |
297 | * ToDS FromDS Addr1 Addr2 Addr3 Addr4 | |
298 | * 0 0 DA SA BSSID n/a | |
299 | * 0 1 DA BSSID SA n/a | |
300 | * 1 0 BSSID SA DA n/a | |
301 | * 1 1 RA TA DA SA | |
302 | */ | |
303 | memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN); | |
304 | memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN); | |
305 | ||
306 | switch (hdr->frame_control & | |
307 | cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) { | |
308 | case cpu_to_le16(IEEE80211_FCTL_TODS): | |
309 | if (unlikely(iftype != NL80211_IFTYPE_AP && | |
310 | iftype != NL80211_IFTYPE_AP_VLAN)) | |
311 | return -1; | |
312 | break; | |
313 | case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS): | |
314 | if (unlikely(iftype != NL80211_IFTYPE_WDS && | |
315 | iftype != NL80211_IFTYPE_MESH_POINT)) | |
316 | return -1; | |
317 | if (iftype == NL80211_IFTYPE_MESH_POINT) { | |
318 | struct ieee80211s_hdr *meshdr = | |
319 | (struct ieee80211s_hdr *) (skb->data + hdrlen); | |
320 | hdrlen += ieee80211_get_mesh_hdrlen(meshdr); | |
321 | if (meshdr->flags & MESH_FLAGS_AE_A5_A6) { | |
322 | memcpy(dst, meshdr->eaddr1, ETH_ALEN); | |
323 | memcpy(src, meshdr->eaddr2, ETH_ALEN); | |
324 | } | |
325 | } | |
326 | break; | |
327 | case cpu_to_le16(IEEE80211_FCTL_FROMDS): | |
328 | if (iftype != NL80211_IFTYPE_STATION || | |
329 | (is_multicast_ether_addr(dst) && | |
330 | !compare_ether_addr(src, addr))) | |
331 | return -1; | |
332 | break; | |
333 | case cpu_to_le16(0): | |
334 | if (iftype != NL80211_IFTYPE_ADHOC) | |
335 | return -1; | |
336 | break; | |
337 | } | |
338 | ||
339 | if (unlikely(skb->len - hdrlen < 8)) | |
340 | return -1; | |
341 | ||
342 | payload = skb->data + hdrlen; | |
343 | ethertype = (payload[6] << 8) | payload[7]; | |
344 | ||
345 | if (likely((compare_ether_addr(payload, rfc1042_header) == 0 && | |
346 | ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || | |
347 | compare_ether_addr(payload, bridge_tunnel_header) == 0)) { | |
348 | /* remove RFC1042 or Bridge-Tunnel encapsulation and | |
349 | * replace EtherType */ | |
350 | skb_pull(skb, hdrlen + 6); | |
351 | memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN); | |
352 | memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN); | |
353 | } else { | |
354 | struct ethhdr *ehdr; | |
355 | __be16 len; | |
356 | ||
357 | skb_pull(skb, hdrlen); | |
358 | len = htons(skb->len); | |
359 | ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr)); | |
360 | memcpy(ehdr->h_dest, dst, ETH_ALEN); | |
361 | memcpy(ehdr->h_source, src, ETH_ALEN); | |
362 | ehdr->h_proto = len; | |
363 | } | |
364 | return 0; | |
365 | } | |
366 | EXPORT_SYMBOL(ieee80211_data_to_8023); | |
367 | ||
368 | int ieee80211_data_from_8023(struct sk_buff *skb, u8 *addr, | |
369 | enum nl80211_iftype iftype, u8 *bssid, bool qos) | |
370 | { | |
371 | struct ieee80211_hdr hdr; | |
372 | u16 hdrlen, ethertype; | |
373 | __le16 fc; | |
374 | const u8 *encaps_data; | |
375 | int encaps_len, skip_header_bytes; | |
376 | int nh_pos, h_pos; | |
377 | int head_need; | |
378 | ||
379 | if (unlikely(skb->len < ETH_HLEN)) | |
380 | return -EINVAL; | |
381 | ||
382 | nh_pos = skb_network_header(skb) - skb->data; | |
383 | h_pos = skb_transport_header(skb) - skb->data; | |
384 | ||
385 | /* convert Ethernet header to proper 802.11 header (based on | |
386 | * operation mode) */ | |
387 | ethertype = (skb->data[12] << 8) | skb->data[13]; | |
388 | fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA); | |
389 | ||
390 | switch (iftype) { | |
391 | case NL80211_IFTYPE_AP: | |
392 | case NL80211_IFTYPE_AP_VLAN: | |
393 | fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS); | |
394 | /* DA BSSID SA */ | |
395 | memcpy(hdr.addr1, skb->data, ETH_ALEN); | |
396 | memcpy(hdr.addr2, addr, ETH_ALEN); | |
397 | memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN); | |
398 | hdrlen = 24; | |
399 | break; | |
400 | case NL80211_IFTYPE_STATION: | |
401 | fc |= cpu_to_le16(IEEE80211_FCTL_TODS); | |
402 | /* BSSID SA DA */ | |
403 | memcpy(hdr.addr1, bssid, ETH_ALEN); | |
404 | memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); | |
405 | memcpy(hdr.addr3, skb->data, ETH_ALEN); | |
406 | hdrlen = 24; | |
407 | break; | |
408 | case NL80211_IFTYPE_ADHOC: | |
409 | /* DA SA BSSID */ | |
410 | memcpy(hdr.addr1, skb->data, ETH_ALEN); | |
411 | memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); | |
412 | memcpy(hdr.addr3, bssid, ETH_ALEN); | |
413 | hdrlen = 24; | |
414 | break; | |
415 | default: | |
416 | return -EOPNOTSUPP; | |
417 | } | |
418 | ||
419 | if (qos) { | |
420 | fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA); | |
421 | hdrlen += 2; | |
422 | } | |
423 | ||
424 | hdr.frame_control = fc; | |
425 | hdr.duration_id = 0; | |
426 | hdr.seq_ctrl = 0; | |
427 | ||
428 | skip_header_bytes = ETH_HLEN; | |
429 | if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) { | |
430 | encaps_data = bridge_tunnel_header; | |
431 | encaps_len = sizeof(bridge_tunnel_header); | |
432 | skip_header_bytes -= 2; | |
433 | } else if (ethertype > 0x600) { | |
434 | encaps_data = rfc1042_header; | |
435 | encaps_len = sizeof(rfc1042_header); | |
436 | skip_header_bytes -= 2; | |
437 | } else { | |
438 | encaps_data = NULL; | |
439 | encaps_len = 0; | |
440 | } | |
441 | ||
442 | skb_pull(skb, skip_header_bytes); | |
443 | nh_pos -= skip_header_bytes; | |
444 | h_pos -= skip_header_bytes; | |
445 | ||
446 | head_need = hdrlen + encaps_len - skb_headroom(skb); | |
447 | ||
448 | if (head_need > 0 || skb_cloned(skb)) { | |
449 | head_need = max(head_need, 0); | |
450 | if (head_need) | |
451 | skb_orphan(skb); | |
452 | ||
453 | if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) { | |
454 | printk(KERN_ERR "failed to reallocate Tx buffer\n"); | |
455 | return -ENOMEM; | |
456 | } | |
457 | skb->truesize += head_need; | |
458 | } | |
459 | ||
460 | if (encaps_data) { | |
461 | memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len); | |
462 | nh_pos += encaps_len; | |
463 | h_pos += encaps_len; | |
464 | } | |
465 | ||
466 | memcpy(skb_push(skb, hdrlen), &hdr, hdrlen); | |
467 | ||
468 | nh_pos += hdrlen; | |
469 | h_pos += hdrlen; | |
470 | ||
471 | /* Update skb pointers to various headers since this modified frame | |
472 | * is going to go through Linux networking code that may potentially | |
473 | * need things like pointer to IP header. */ | |
474 | skb_set_mac_header(skb, 0); | |
475 | skb_set_network_header(skb, nh_pos); | |
476 | skb_set_transport_header(skb, h_pos); | |
477 | ||
478 | return 0; | |
479 | } | |
480 | EXPORT_SYMBOL(ieee80211_data_from_8023); | |
481 | ||
482 | /* Given a data frame determine the 802.1p/1d tag to use. */ | |
483 | unsigned int cfg80211_classify8021d(struct sk_buff *skb) | |
484 | { | |
485 | unsigned int dscp; | |
486 | ||
487 | /* skb->priority values from 256->263 are magic values to | |
488 | * directly indicate a specific 802.1d priority. This is used | |
489 | * to allow 802.1d priority to be passed directly in from VLAN | |
490 | * tags, etc. | |
491 | */ | |
492 | if (skb->priority >= 256 && skb->priority <= 263) | |
493 | return skb->priority - 256; | |
494 | ||
495 | switch (skb->protocol) { | |
496 | case htons(ETH_P_IP): | |
497 | dscp = ip_hdr(skb)->tos & 0xfc; | |
498 | break; | |
499 | default: | |
500 | return 0; | |
501 | } | |
502 | ||
503 | return dscp >> 5; | |
504 | } | |
505 | EXPORT_SYMBOL(cfg80211_classify8021d); |