util.c 12 KB

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