util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 <linux/slab.h>
  9. #include <net/cfg80211.h>
  10. #include <net/ip.h>
  11. #include "core.h"
  12. struct ieee80211_rate *
  13. ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  14. u32 basic_rates, int bitrate)
  15. {
  16. struct ieee80211_rate *result = &sband->bitrates[0];
  17. int i;
  18. for (i = 0; i < sband->n_bitrates; i++) {
  19. if (!(basic_rates & BIT(i)))
  20. continue;
  21. if (sband->bitrates[i].bitrate > bitrate)
  22. continue;
  23. result = &sband->bitrates[i];
  24. }
  25. return result;
  26. }
  27. EXPORT_SYMBOL(ieee80211_get_response_rate);
  28. int ieee80211_channel_to_frequency(int chan)
  29. {
  30. if (chan < 14)
  31. return 2407 + chan * 5;
  32. if (chan == 14)
  33. return 2484;
  34. /* FIXME: 802.11j 17.3.8.3.2 */
  35. return (chan + 1000) * 5;
  36. }
  37. EXPORT_SYMBOL(ieee80211_channel_to_frequency);
  38. int ieee80211_frequency_to_channel(int freq)
  39. {
  40. if (freq == 2484)
  41. return 14;
  42. if (freq < 2484)
  43. return (freq - 2407) / 5;
  44. /* FIXME: 802.11j 17.3.8.3.2 */
  45. return freq/5 - 1000;
  46. }
  47. EXPORT_SYMBOL(ieee80211_frequency_to_channel);
  48. struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
  49. int freq)
  50. {
  51. enum ieee80211_band band;
  52. struct ieee80211_supported_band *sband;
  53. int i;
  54. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  55. sband = wiphy->bands[band];
  56. if (!sband)
  57. continue;
  58. for (i = 0; i < sband->n_channels; i++) {
  59. if (sband->channels[i].center_freq == freq)
  60. return &sband->channels[i];
  61. }
  62. }
  63. return NULL;
  64. }
  65. EXPORT_SYMBOL(__ieee80211_get_channel);
  66. static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
  67. enum ieee80211_band band)
  68. {
  69. int i, want;
  70. switch (band) {
  71. case IEEE80211_BAND_5GHZ:
  72. want = 3;
  73. for (i = 0; i < sband->n_bitrates; i++) {
  74. if (sband->bitrates[i].bitrate == 60 ||
  75. sband->bitrates[i].bitrate == 120 ||
  76. sband->bitrates[i].bitrate == 240) {
  77. sband->bitrates[i].flags |=
  78. IEEE80211_RATE_MANDATORY_A;
  79. want--;
  80. }
  81. }
  82. WARN_ON(want);
  83. break;
  84. case IEEE80211_BAND_2GHZ:
  85. want = 7;
  86. for (i = 0; i < sband->n_bitrates; i++) {
  87. if (sband->bitrates[i].bitrate == 10) {
  88. sband->bitrates[i].flags |=
  89. IEEE80211_RATE_MANDATORY_B |
  90. IEEE80211_RATE_MANDATORY_G;
  91. want--;
  92. }
  93. if (sband->bitrates[i].bitrate == 20 ||
  94. sband->bitrates[i].bitrate == 55 ||
  95. sband->bitrates[i].bitrate == 110 ||
  96. sband->bitrates[i].bitrate == 60 ||
  97. sband->bitrates[i].bitrate == 120 ||
  98. sband->bitrates[i].bitrate == 240) {
  99. sband->bitrates[i].flags |=
  100. IEEE80211_RATE_MANDATORY_G;
  101. want--;
  102. }
  103. if (sband->bitrates[i].bitrate != 10 &&
  104. sband->bitrates[i].bitrate != 20 &&
  105. sband->bitrates[i].bitrate != 55 &&
  106. sband->bitrates[i].bitrate != 110)
  107. sband->bitrates[i].flags |=
  108. IEEE80211_RATE_ERP_G;
  109. }
  110. WARN_ON(want != 0 && want != 3 && want != 6);
  111. break;
  112. case IEEE80211_NUM_BANDS:
  113. WARN_ON(1);
  114. break;
  115. }
  116. }
  117. void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
  118. {
  119. enum ieee80211_band band;
  120. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  121. if (wiphy->bands[band])
  122. set_mandatory_flags_band(wiphy->bands[band], band);
  123. }
  124. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  125. struct key_params *params, int key_idx,
  126. const u8 *mac_addr)
  127. {
  128. int i;
  129. if (key_idx > 5)
  130. return -EINVAL;
  131. /*
  132. * Disallow pairwise keys with non-zero index unless it's WEP
  133. * (because current deployments use pairwise WEP keys with
  134. * non-zero indizes but 802.11i clearly specifies to use zero)
  135. */
  136. if (mac_addr && key_idx &&
  137. params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
  138. params->cipher != WLAN_CIPHER_SUITE_WEP104)
  139. return -EINVAL;
  140. switch (params->cipher) {
  141. case WLAN_CIPHER_SUITE_WEP40:
  142. if (params->key_len != WLAN_KEY_LEN_WEP40)
  143. return -EINVAL;
  144. break;
  145. case WLAN_CIPHER_SUITE_TKIP:
  146. if (params->key_len != WLAN_KEY_LEN_TKIP)
  147. return -EINVAL;
  148. break;
  149. case WLAN_CIPHER_SUITE_CCMP:
  150. if (params->key_len != WLAN_KEY_LEN_CCMP)
  151. return -EINVAL;
  152. break;
  153. case WLAN_CIPHER_SUITE_WEP104:
  154. if (params->key_len != WLAN_KEY_LEN_WEP104)
  155. return -EINVAL;
  156. break;
  157. case WLAN_CIPHER_SUITE_AES_CMAC:
  158. if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
  159. return -EINVAL;
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. if (params->seq) {
  165. switch (params->cipher) {
  166. case WLAN_CIPHER_SUITE_WEP40:
  167. case WLAN_CIPHER_SUITE_WEP104:
  168. /* These ciphers do not use key sequence */
  169. return -EINVAL;
  170. case WLAN_CIPHER_SUITE_TKIP:
  171. case WLAN_CIPHER_SUITE_CCMP:
  172. case WLAN_CIPHER_SUITE_AES_CMAC:
  173. if (params->seq_len != 6)
  174. return -EINVAL;
  175. break;
  176. }
  177. }
  178. for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
  179. if (params->cipher == rdev->wiphy.cipher_suites[i])
  180. break;
  181. if (i == rdev->wiphy.n_cipher_suites)
  182. return -EINVAL;
  183. return 0;
  184. }
  185. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  186. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  187. const unsigned char rfc1042_header[] __aligned(2) =
  188. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  189. EXPORT_SYMBOL(rfc1042_header);
  190. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  191. const unsigned char bridge_tunnel_header[] __aligned(2) =
  192. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  193. EXPORT_SYMBOL(bridge_tunnel_header);
  194. unsigned int ieee80211_hdrlen(__le16 fc)
  195. {
  196. unsigned int hdrlen = 24;
  197. if (ieee80211_is_data(fc)) {
  198. if (ieee80211_has_a4(fc))
  199. hdrlen = 30;
  200. if (ieee80211_is_data_qos(fc)) {
  201. hdrlen += IEEE80211_QOS_CTL_LEN;
  202. if (ieee80211_has_order(fc))
  203. hdrlen += IEEE80211_HT_CTL_LEN;
  204. }
  205. goto out;
  206. }
  207. if (ieee80211_is_ctl(fc)) {
  208. /*
  209. * ACK and CTS are 10 bytes, all others 16. To see how
  210. * to get this condition consider
  211. * subtype mask: 0b0000000011110000 (0x00F0)
  212. * ACK subtype: 0b0000000011010000 (0x00D0)
  213. * CTS subtype: 0b0000000011000000 (0x00C0)
  214. * bits that matter: ^^^ (0x00E0)
  215. * value of those: 0b0000000011000000 (0x00C0)
  216. */
  217. if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
  218. hdrlen = 10;
  219. else
  220. hdrlen = 16;
  221. }
  222. out:
  223. return hdrlen;
  224. }
  225. EXPORT_SYMBOL(ieee80211_hdrlen);
  226. unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  227. {
  228. const struct ieee80211_hdr *hdr =
  229. (const struct ieee80211_hdr *)skb->data;
  230. unsigned int hdrlen;
  231. if (unlikely(skb->len < 10))
  232. return 0;
  233. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  234. if (unlikely(hdrlen > skb->len))
  235. return 0;
  236. return hdrlen;
  237. }
  238. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  239. static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
  240. {
  241. int ae = meshhdr->flags & MESH_FLAGS_AE;
  242. /* 7.1.3.5a.2 */
  243. switch (ae) {
  244. case 0:
  245. return 6;
  246. case MESH_FLAGS_AE_A4:
  247. return 12;
  248. case MESH_FLAGS_AE_A5_A6:
  249. return 18;
  250. case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
  251. return 24;
  252. default:
  253. return 6;
  254. }
  255. }
  256. int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
  257. enum nl80211_iftype iftype)
  258. {
  259. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  260. u16 hdrlen, ethertype;
  261. u8 *payload;
  262. u8 dst[ETH_ALEN];
  263. u8 src[ETH_ALEN] __aligned(2);
  264. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  265. return -1;
  266. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  267. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  268. * header
  269. * IEEE 802.11 address fields:
  270. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  271. * 0 0 DA SA BSSID n/a
  272. * 0 1 DA BSSID SA n/a
  273. * 1 0 BSSID SA DA n/a
  274. * 1 1 RA TA DA SA
  275. */
  276. memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
  277. memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
  278. switch (hdr->frame_control &
  279. cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  280. case cpu_to_le16(IEEE80211_FCTL_TODS):
  281. if (unlikely(iftype != NL80211_IFTYPE_AP &&
  282. iftype != NL80211_IFTYPE_AP_VLAN))
  283. return -1;
  284. break;
  285. case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  286. if (unlikely(iftype != NL80211_IFTYPE_WDS &&
  287. iftype != NL80211_IFTYPE_MESH_POINT &&
  288. iftype != NL80211_IFTYPE_AP_VLAN &&
  289. iftype != NL80211_IFTYPE_STATION))
  290. return -1;
  291. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  292. struct ieee80211s_hdr *meshdr =
  293. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  294. /* make sure meshdr->flags is on the linear part */
  295. if (!pskb_may_pull(skb, hdrlen + 1))
  296. return -1;
  297. if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
  298. skb_copy_bits(skb, hdrlen +
  299. offsetof(struct ieee80211s_hdr, eaddr1),
  300. dst, ETH_ALEN);
  301. skb_copy_bits(skb, hdrlen +
  302. offsetof(struct ieee80211s_hdr, eaddr2),
  303. src, ETH_ALEN);
  304. }
  305. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  306. }
  307. break;
  308. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  309. if ((iftype != NL80211_IFTYPE_STATION &&
  310. iftype != NL80211_IFTYPE_MESH_POINT) ||
  311. (is_multicast_ether_addr(dst) &&
  312. !compare_ether_addr(src, addr)))
  313. return -1;
  314. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  315. struct ieee80211s_hdr *meshdr =
  316. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  317. /* make sure meshdr->flags is on the linear part */
  318. if (!pskb_may_pull(skb, hdrlen + 1))
  319. return -1;
  320. if (meshdr->flags & MESH_FLAGS_AE_A4)
  321. skb_copy_bits(skb, hdrlen +
  322. offsetof(struct ieee80211s_hdr, eaddr1),
  323. src, ETH_ALEN);
  324. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  325. }
  326. break;
  327. case cpu_to_le16(0):
  328. if (iftype != NL80211_IFTYPE_ADHOC)
  329. return -1;
  330. break;
  331. }
  332. if (!pskb_may_pull(skb, hdrlen + 8))
  333. return -1;
  334. payload = skb->data + hdrlen;
  335. ethertype = (payload[6] << 8) | payload[7];
  336. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  337. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  338. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  339. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  340. * replace EtherType */
  341. skb_pull(skb, hdrlen + 6);
  342. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  343. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  344. } else {
  345. struct ethhdr *ehdr;
  346. __be16 len;
  347. skb_pull(skb, hdrlen);
  348. len = htons(skb->len);
  349. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  350. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  351. memcpy(ehdr->h_source, src, ETH_ALEN);
  352. ehdr->h_proto = len;
  353. }
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(ieee80211_data_to_8023);
  357. int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
  358. enum nl80211_iftype iftype, u8 *bssid, bool qos)
  359. {
  360. struct ieee80211_hdr hdr;
  361. u16 hdrlen, ethertype;
  362. __le16 fc;
  363. const u8 *encaps_data;
  364. int encaps_len, skip_header_bytes;
  365. int nh_pos, h_pos;
  366. int head_need;
  367. if (unlikely(skb->len < ETH_HLEN))
  368. return -EINVAL;
  369. nh_pos = skb_network_header(skb) - skb->data;
  370. h_pos = skb_transport_header(skb) - skb->data;
  371. /* convert Ethernet header to proper 802.11 header (based on
  372. * operation mode) */
  373. ethertype = (skb->data[12] << 8) | skb->data[13];
  374. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  375. switch (iftype) {
  376. case NL80211_IFTYPE_AP:
  377. case NL80211_IFTYPE_AP_VLAN:
  378. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  379. /* DA BSSID SA */
  380. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  381. memcpy(hdr.addr2, addr, ETH_ALEN);
  382. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  383. hdrlen = 24;
  384. break;
  385. case NL80211_IFTYPE_STATION:
  386. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  387. /* BSSID SA DA */
  388. memcpy(hdr.addr1, bssid, ETH_ALEN);
  389. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  390. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  391. hdrlen = 24;
  392. break;
  393. case NL80211_IFTYPE_ADHOC:
  394. /* DA SA BSSID */
  395. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  396. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  397. memcpy(hdr.addr3, bssid, ETH_ALEN);
  398. hdrlen = 24;
  399. break;
  400. default:
  401. return -EOPNOTSUPP;
  402. }
  403. if (qos) {
  404. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  405. hdrlen += 2;
  406. }
  407. hdr.frame_control = fc;
  408. hdr.duration_id = 0;
  409. hdr.seq_ctrl = 0;
  410. skip_header_bytes = ETH_HLEN;
  411. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  412. encaps_data = bridge_tunnel_header;
  413. encaps_len = sizeof(bridge_tunnel_header);
  414. skip_header_bytes -= 2;
  415. } else if (ethertype > 0x600) {
  416. encaps_data = rfc1042_header;
  417. encaps_len = sizeof(rfc1042_header);
  418. skip_header_bytes -= 2;
  419. } else {
  420. encaps_data = NULL;
  421. encaps_len = 0;
  422. }
  423. skb_pull(skb, skip_header_bytes);
  424. nh_pos -= skip_header_bytes;
  425. h_pos -= skip_header_bytes;
  426. head_need = hdrlen + encaps_len - skb_headroom(skb);
  427. if (head_need > 0 || skb_cloned(skb)) {
  428. head_need = max(head_need, 0);
  429. if (head_need)
  430. skb_orphan(skb);
  431. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
  432. printk(KERN_ERR "failed to reallocate Tx buffer\n");
  433. return -ENOMEM;
  434. }
  435. skb->truesize += head_need;
  436. }
  437. if (encaps_data) {
  438. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  439. nh_pos += encaps_len;
  440. h_pos += encaps_len;
  441. }
  442. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  443. nh_pos += hdrlen;
  444. h_pos += hdrlen;
  445. /* Update skb pointers to various headers since this modified frame
  446. * is going to go through Linux networking code that may potentially
  447. * need things like pointer to IP header. */
  448. skb_set_mac_header(skb, 0);
  449. skb_set_network_header(skb, nh_pos);
  450. skb_set_transport_header(skb, h_pos);
  451. return 0;
  452. }
  453. EXPORT_SYMBOL(ieee80211_data_from_8023);
  454. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  455. const u8 *addr, enum nl80211_iftype iftype,
  456. const unsigned int extra_headroom)
  457. {
  458. struct sk_buff *frame = NULL;
  459. u16 ethertype;
  460. u8 *payload;
  461. const struct ethhdr *eth;
  462. int remaining, err;
  463. u8 dst[ETH_ALEN], src[ETH_ALEN];
  464. err = ieee80211_data_to_8023(skb, addr, iftype);
  465. if (err)
  466. goto out;
  467. /* skip the wrapping header */
  468. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  469. if (!eth)
  470. goto out;
  471. while (skb != frame) {
  472. u8 padding;
  473. __be16 len = eth->h_proto;
  474. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  475. remaining = skb->len;
  476. memcpy(dst, eth->h_dest, ETH_ALEN);
  477. memcpy(src, eth->h_source, ETH_ALEN);
  478. padding = (4 - subframe_len) & 0x3;
  479. /* the last MSDU has no padding */
  480. if (subframe_len > remaining)
  481. goto purge;
  482. skb_pull(skb, sizeof(struct ethhdr));
  483. /* reuse skb for the last subframe */
  484. if (remaining <= subframe_len + padding)
  485. frame = skb;
  486. else {
  487. unsigned int hlen = ALIGN(extra_headroom, 4);
  488. /*
  489. * Allocate and reserve two bytes more for payload
  490. * alignment since sizeof(struct ethhdr) is 14.
  491. */
  492. frame = dev_alloc_skb(hlen + subframe_len + 2);
  493. if (!frame)
  494. goto purge;
  495. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  496. memcpy(skb_put(frame, ntohs(len)), skb->data,
  497. ntohs(len));
  498. eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
  499. padding);
  500. if (!eth) {
  501. dev_kfree_skb(frame);
  502. goto purge;
  503. }
  504. }
  505. skb_reset_network_header(frame);
  506. frame->dev = skb->dev;
  507. frame->priority = skb->priority;
  508. payload = frame->data;
  509. ethertype = (payload[6] << 8) | payload[7];
  510. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  511. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  512. compare_ether_addr(payload,
  513. bridge_tunnel_header) == 0)) {
  514. /* remove RFC1042 or Bridge-Tunnel
  515. * encapsulation and replace EtherType */
  516. skb_pull(frame, 6);
  517. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  518. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  519. } else {
  520. memcpy(skb_push(frame, sizeof(__be16)), &len,
  521. sizeof(__be16));
  522. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  523. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  524. }
  525. __skb_queue_tail(list, frame);
  526. }
  527. return;
  528. purge:
  529. __skb_queue_purge(list);
  530. out:
  531. dev_kfree_skb(skb);
  532. }
  533. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  534. /* Given a data frame determine the 802.1p/1d tag to use. */
  535. unsigned int cfg80211_classify8021d(struct sk_buff *skb)
  536. {
  537. unsigned int dscp;
  538. /* skb->priority values from 256->263 are magic values to
  539. * directly indicate a specific 802.1d priority. This is used
  540. * to allow 802.1d priority to be passed directly in from VLAN
  541. * tags, etc.
  542. */
  543. if (skb->priority >= 256 && skb->priority <= 263)
  544. return skb->priority - 256;
  545. switch (skb->protocol) {
  546. case htons(ETH_P_IP):
  547. dscp = ip_hdr(skb)->tos & 0xfc;
  548. break;
  549. default:
  550. return 0;
  551. }
  552. return dscp >> 5;
  553. }
  554. EXPORT_SYMBOL(cfg80211_classify8021d);
  555. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  556. {
  557. u8 *end, *pos;
  558. pos = bss->information_elements;
  559. if (pos == NULL)
  560. return NULL;
  561. end = pos + bss->len_information_elements;
  562. while (pos + 1 < end) {
  563. if (pos + 2 + pos[1] > end)
  564. break;
  565. if (pos[0] == ie)
  566. return pos;
  567. pos += 2 + pos[1];
  568. }
  569. return NULL;
  570. }
  571. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  572. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  573. {
  574. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  575. struct net_device *dev = wdev->netdev;
  576. int i;
  577. if (!wdev->connect_keys)
  578. return;
  579. for (i = 0; i < 6; i++) {
  580. if (!wdev->connect_keys->params[i].cipher)
  581. continue;
  582. if (rdev->ops->add_key(wdev->wiphy, dev, i, NULL,
  583. &wdev->connect_keys->params[i])) {
  584. printk(KERN_ERR "%s: failed to set key %d\n",
  585. dev->name, i);
  586. continue;
  587. }
  588. if (wdev->connect_keys->def == i)
  589. if (rdev->ops->set_default_key(wdev->wiphy, dev, i)) {
  590. printk(KERN_ERR "%s: failed to set defkey %d\n",
  591. dev->name, i);
  592. continue;
  593. }
  594. if (wdev->connect_keys->defmgmt == i)
  595. if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
  596. printk(KERN_ERR "%s: failed to set mgtdef %d\n",
  597. dev->name, i);
  598. }
  599. kfree(wdev->connect_keys);
  600. wdev->connect_keys = NULL;
  601. }
  602. static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  603. {
  604. struct cfg80211_event *ev;
  605. unsigned long flags;
  606. const u8 *bssid = NULL;
  607. spin_lock_irqsave(&wdev->event_lock, flags);
  608. while (!list_empty(&wdev->event_list)) {
  609. ev = list_first_entry(&wdev->event_list,
  610. struct cfg80211_event, list);
  611. list_del(&ev->list);
  612. spin_unlock_irqrestore(&wdev->event_lock, flags);
  613. wdev_lock(wdev);
  614. switch (ev->type) {
  615. case EVENT_CONNECT_RESULT:
  616. if (!is_zero_ether_addr(ev->cr.bssid))
  617. bssid = ev->cr.bssid;
  618. __cfg80211_connect_result(
  619. wdev->netdev, bssid,
  620. ev->cr.req_ie, ev->cr.req_ie_len,
  621. ev->cr.resp_ie, ev->cr.resp_ie_len,
  622. ev->cr.status,
  623. ev->cr.status == WLAN_STATUS_SUCCESS,
  624. NULL);
  625. break;
  626. case EVENT_ROAMED:
  627. __cfg80211_roamed(wdev, ev->rm.bssid,
  628. ev->rm.req_ie, ev->rm.req_ie_len,
  629. ev->rm.resp_ie, ev->rm.resp_ie_len);
  630. break;
  631. case EVENT_DISCONNECTED:
  632. __cfg80211_disconnected(wdev->netdev,
  633. ev->dc.ie, ev->dc.ie_len,
  634. ev->dc.reason, true);
  635. break;
  636. case EVENT_IBSS_JOINED:
  637. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
  638. break;
  639. }
  640. wdev_unlock(wdev);
  641. kfree(ev);
  642. spin_lock_irqsave(&wdev->event_lock, flags);
  643. }
  644. spin_unlock_irqrestore(&wdev->event_lock, flags);
  645. }
  646. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  647. {
  648. struct wireless_dev *wdev;
  649. ASSERT_RTNL();
  650. ASSERT_RDEV_LOCK(rdev);
  651. mutex_lock(&rdev->devlist_mtx);
  652. list_for_each_entry(wdev, &rdev->netdev_list, list)
  653. cfg80211_process_wdev_events(wdev);
  654. mutex_unlock(&rdev->devlist_mtx);
  655. }
  656. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  657. struct net_device *dev, enum nl80211_iftype ntype,
  658. u32 *flags, struct vif_params *params)
  659. {
  660. int err;
  661. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  662. ASSERT_RDEV_LOCK(rdev);
  663. /* don't support changing VLANs, you just re-create them */
  664. if (otype == NL80211_IFTYPE_AP_VLAN)
  665. return -EOPNOTSUPP;
  666. if (!rdev->ops->change_virtual_intf ||
  667. !(rdev->wiphy.interface_modes & (1 << ntype)))
  668. return -EOPNOTSUPP;
  669. /* if it's part of a bridge, reject changing type to station/ibss */
  670. if (dev->br_port && (ntype == NL80211_IFTYPE_ADHOC ||
  671. ntype == NL80211_IFTYPE_STATION))
  672. return -EBUSY;
  673. if (ntype != otype) {
  674. dev->ieee80211_ptr->use_4addr = false;
  675. switch (otype) {
  676. case NL80211_IFTYPE_ADHOC:
  677. cfg80211_leave_ibss(rdev, dev, false);
  678. break;
  679. case NL80211_IFTYPE_STATION:
  680. cfg80211_disconnect(rdev, dev,
  681. WLAN_REASON_DEAUTH_LEAVING, true);
  682. break;
  683. case NL80211_IFTYPE_MESH_POINT:
  684. /* mesh should be handled? */
  685. break;
  686. default:
  687. break;
  688. }
  689. cfg80211_process_rdev_events(rdev);
  690. }
  691. err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
  692. ntype, flags, params);
  693. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  694. if (!err && params && params->use_4addr != -1)
  695. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  696. if (!err) {
  697. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  698. switch (ntype) {
  699. case NL80211_IFTYPE_STATION:
  700. if (dev->ieee80211_ptr->use_4addr)
  701. break;
  702. /* fall through */
  703. case NL80211_IFTYPE_ADHOC:
  704. dev->priv_flags |= IFF_DONT_BRIDGE;
  705. break;
  706. case NL80211_IFTYPE_AP:
  707. case NL80211_IFTYPE_AP_VLAN:
  708. case NL80211_IFTYPE_WDS:
  709. case NL80211_IFTYPE_MESH_POINT:
  710. /* bridging OK */
  711. break;
  712. case NL80211_IFTYPE_MONITOR:
  713. /* monitor can't bridge anyway */
  714. break;
  715. case NL80211_IFTYPE_UNSPECIFIED:
  716. case __NL80211_IFTYPE_AFTER_LAST:
  717. /* not happening */
  718. break;
  719. }
  720. }
  721. return err;
  722. }
  723. u16 cfg80211_calculate_bitrate(struct rate_info *rate)
  724. {
  725. int modulation, streams, bitrate;
  726. if (!(rate->flags & RATE_INFO_FLAGS_MCS))
  727. return rate->legacy;
  728. /* the formula below does only work for MCS values smaller than 32 */
  729. if (rate->mcs >= 32)
  730. return 0;
  731. modulation = rate->mcs & 7;
  732. streams = (rate->mcs >> 3) + 1;
  733. bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
  734. 13500000 : 6500000;
  735. if (modulation < 4)
  736. bitrate *= (modulation + 1);
  737. else if (modulation == 4)
  738. bitrate *= (modulation + 2);
  739. else
  740. bitrate *= (modulation + 3);
  741. bitrate *= streams;
  742. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  743. bitrate = (bitrate / 9) * 10;
  744. /* do NOT round down here */
  745. return (bitrate + 50000) / 100000;
  746. }