util.c 21 KB

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