util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  295. if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
  296. memcpy(dst, meshdr->eaddr1, ETH_ALEN);
  297. memcpy(src, meshdr->eaddr2, ETH_ALEN);
  298. }
  299. }
  300. break;
  301. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  302. if ((iftype != NL80211_IFTYPE_STATION &&
  303. iftype != NL80211_IFTYPE_MESH_POINT) ||
  304. (is_multicast_ether_addr(dst) &&
  305. !compare_ether_addr(src, addr)))
  306. return -1;
  307. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  308. struct ieee80211s_hdr *meshdr =
  309. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  310. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  311. if (meshdr->flags & MESH_FLAGS_AE_A4)
  312. memcpy(src, meshdr->eaddr1, ETH_ALEN);
  313. }
  314. break;
  315. case cpu_to_le16(0):
  316. if (iftype != NL80211_IFTYPE_ADHOC)
  317. return -1;
  318. break;
  319. }
  320. if (unlikely(skb->len - hdrlen < 8))
  321. return -1;
  322. payload = skb->data + hdrlen;
  323. ethertype = (payload[6] << 8) | payload[7];
  324. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  325. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  326. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  327. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  328. * replace EtherType */
  329. skb_pull(skb, hdrlen + 6);
  330. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  331. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  332. } else {
  333. struct ethhdr *ehdr;
  334. __be16 len;
  335. skb_pull(skb, hdrlen);
  336. len = htons(skb->len);
  337. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  338. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  339. memcpy(ehdr->h_source, src, ETH_ALEN);
  340. ehdr->h_proto = len;
  341. }
  342. return 0;
  343. }
  344. EXPORT_SYMBOL(ieee80211_data_to_8023);
  345. int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
  346. enum nl80211_iftype iftype, u8 *bssid, bool qos)
  347. {
  348. struct ieee80211_hdr hdr;
  349. u16 hdrlen, ethertype;
  350. __le16 fc;
  351. const u8 *encaps_data;
  352. int encaps_len, skip_header_bytes;
  353. int nh_pos, h_pos;
  354. int head_need;
  355. if (unlikely(skb->len < ETH_HLEN))
  356. return -EINVAL;
  357. nh_pos = skb_network_header(skb) - skb->data;
  358. h_pos = skb_transport_header(skb) - skb->data;
  359. /* convert Ethernet header to proper 802.11 header (based on
  360. * operation mode) */
  361. ethertype = (skb->data[12] << 8) | skb->data[13];
  362. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  363. switch (iftype) {
  364. case NL80211_IFTYPE_AP:
  365. case NL80211_IFTYPE_AP_VLAN:
  366. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  367. /* DA BSSID SA */
  368. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  369. memcpy(hdr.addr2, addr, ETH_ALEN);
  370. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  371. hdrlen = 24;
  372. break;
  373. case NL80211_IFTYPE_STATION:
  374. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  375. /* BSSID SA DA */
  376. memcpy(hdr.addr1, bssid, ETH_ALEN);
  377. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  378. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  379. hdrlen = 24;
  380. break;
  381. case NL80211_IFTYPE_ADHOC:
  382. /* DA SA BSSID */
  383. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  384. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  385. memcpy(hdr.addr3, bssid, ETH_ALEN);
  386. hdrlen = 24;
  387. break;
  388. default:
  389. return -EOPNOTSUPP;
  390. }
  391. if (qos) {
  392. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  393. hdrlen += 2;
  394. }
  395. hdr.frame_control = fc;
  396. hdr.duration_id = 0;
  397. hdr.seq_ctrl = 0;
  398. skip_header_bytes = ETH_HLEN;
  399. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  400. encaps_data = bridge_tunnel_header;
  401. encaps_len = sizeof(bridge_tunnel_header);
  402. skip_header_bytes -= 2;
  403. } else if (ethertype > 0x600) {
  404. encaps_data = rfc1042_header;
  405. encaps_len = sizeof(rfc1042_header);
  406. skip_header_bytes -= 2;
  407. } else {
  408. encaps_data = NULL;
  409. encaps_len = 0;
  410. }
  411. skb_pull(skb, skip_header_bytes);
  412. nh_pos -= skip_header_bytes;
  413. h_pos -= skip_header_bytes;
  414. head_need = hdrlen + encaps_len - skb_headroom(skb);
  415. if (head_need > 0 || skb_cloned(skb)) {
  416. head_need = max(head_need, 0);
  417. if (head_need)
  418. skb_orphan(skb);
  419. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
  420. printk(KERN_ERR "failed to reallocate Tx buffer\n");
  421. return -ENOMEM;
  422. }
  423. skb->truesize += head_need;
  424. }
  425. if (encaps_data) {
  426. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  427. nh_pos += encaps_len;
  428. h_pos += encaps_len;
  429. }
  430. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  431. nh_pos += hdrlen;
  432. h_pos += hdrlen;
  433. /* Update skb pointers to various headers since this modified frame
  434. * is going to go through Linux networking code that may potentially
  435. * need things like pointer to IP header. */
  436. skb_set_mac_header(skb, 0);
  437. skb_set_network_header(skb, nh_pos);
  438. skb_set_transport_header(skb, h_pos);
  439. return 0;
  440. }
  441. EXPORT_SYMBOL(ieee80211_data_from_8023);
  442. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  443. const u8 *addr, enum nl80211_iftype iftype,
  444. const unsigned int extra_headroom)
  445. {
  446. struct sk_buff *frame = NULL;
  447. u16 ethertype;
  448. u8 *payload;
  449. const struct ethhdr *eth;
  450. int remaining, err;
  451. u8 dst[ETH_ALEN], src[ETH_ALEN];
  452. err = ieee80211_data_to_8023(skb, addr, iftype);
  453. if (err)
  454. goto out;
  455. /* skip the wrapping header */
  456. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  457. if (!eth)
  458. goto out;
  459. while (skb != frame) {
  460. u8 padding;
  461. __be16 len = eth->h_proto;
  462. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  463. remaining = skb->len;
  464. memcpy(dst, eth->h_dest, ETH_ALEN);
  465. memcpy(src, eth->h_source, ETH_ALEN);
  466. padding = (4 - subframe_len) & 0x3;
  467. /* the last MSDU has no padding */
  468. if (subframe_len > remaining)
  469. goto purge;
  470. skb_pull(skb, sizeof(struct ethhdr));
  471. /* reuse skb for the last subframe */
  472. if (remaining <= subframe_len + padding)
  473. frame = skb;
  474. else {
  475. unsigned int hlen = ALIGN(extra_headroom, 4);
  476. /*
  477. * Allocate and reserve two bytes more for payload
  478. * alignment since sizeof(struct ethhdr) is 14.
  479. */
  480. frame = dev_alloc_skb(hlen + subframe_len + 2);
  481. if (!frame)
  482. goto purge;
  483. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  484. memcpy(skb_put(frame, ntohs(len)), skb->data,
  485. ntohs(len));
  486. eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
  487. padding);
  488. if (!eth) {
  489. dev_kfree_skb(frame);
  490. goto purge;
  491. }
  492. }
  493. skb_reset_network_header(frame);
  494. frame->dev = skb->dev;
  495. frame->priority = skb->priority;
  496. payload = frame->data;
  497. ethertype = (payload[6] << 8) | payload[7];
  498. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  499. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  500. compare_ether_addr(payload,
  501. bridge_tunnel_header) == 0)) {
  502. /* remove RFC1042 or Bridge-Tunnel
  503. * encapsulation and replace EtherType */
  504. skb_pull(frame, 6);
  505. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  506. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  507. } else {
  508. memcpy(skb_push(frame, sizeof(__be16)), &len,
  509. sizeof(__be16));
  510. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  511. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  512. }
  513. __skb_queue_tail(list, frame);
  514. }
  515. return;
  516. purge:
  517. __skb_queue_purge(list);
  518. out:
  519. dev_kfree_skb(skb);
  520. }
  521. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  522. /* Given a data frame determine the 802.1p/1d tag to use. */
  523. unsigned int cfg80211_classify8021d(struct sk_buff *skb)
  524. {
  525. unsigned int dscp;
  526. /* skb->priority values from 256->263 are magic values to
  527. * directly indicate a specific 802.1d priority. This is used
  528. * to allow 802.1d priority to be passed directly in from VLAN
  529. * tags, etc.
  530. */
  531. if (skb->priority >= 256 && skb->priority <= 263)
  532. return skb->priority - 256;
  533. switch (skb->protocol) {
  534. case htons(ETH_P_IP):
  535. dscp = ip_hdr(skb)->tos & 0xfc;
  536. break;
  537. default:
  538. return 0;
  539. }
  540. return dscp >> 5;
  541. }
  542. EXPORT_SYMBOL(cfg80211_classify8021d);
  543. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  544. {
  545. u8 *end, *pos;
  546. pos = bss->information_elements;
  547. if (pos == NULL)
  548. return NULL;
  549. end = pos + bss->len_information_elements;
  550. while (pos + 1 < end) {
  551. if (pos + 2 + pos[1] > end)
  552. break;
  553. if (pos[0] == ie)
  554. return pos;
  555. pos += 2 + pos[1];
  556. }
  557. return NULL;
  558. }
  559. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  560. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  561. {
  562. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  563. struct net_device *dev = wdev->netdev;
  564. int i;
  565. if (!wdev->connect_keys)
  566. return;
  567. for (i = 0; i < 6; i++) {
  568. if (!wdev->connect_keys->params[i].cipher)
  569. continue;
  570. if (rdev->ops->add_key(wdev->wiphy, dev, i, NULL,
  571. &wdev->connect_keys->params[i])) {
  572. printk(KERN_ERR "%s: failed to set key %d\n",
  573. dev->name, i);
  574. continue;
  575. }
  576. if (wdev->connect_keys->def == i)
  577. if (rdev->ops->set_default_key(wdev->wiphy, dev, i)) {
  578. printk(KERN_ERR "%s: failed to set defkey %d\n",
  579. dev->name, i);
  580. continue;
  581. }
  582. if (wdev->connect_keys->defmgmt == i)
  583. if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
  584. printk(KERN_ERR "%s: failed to set mgtdef %d\n",
  585. dev->name, i);
  586. }
  587. kfree(wdev->connect_keys);
  588. wdev->connect_keys = NULL;
  589. }
  590. static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  591. {
  592. struct cfg80211_event *ev;
  593. unsigned long flags;
  594. const u8 *bssid = NULL;
  595. spin_lock_irqsave(&wdev->event_lock, flags);
  596. while (!list_empty(&wdev->event_list)) {
  597. ev = list_first_entry(&wdev->event_list,
  598. struct cfg80211_event, list);
  599. list_del(&ev->list);
  600. spin_unlock_irqrestore(&wdev->event_lock, flags);
  601. wdev_lock(wdev);
  602. switch (ev->type) {
  603. case EVENT_CONNECT_RESULT:
  604. if (!is_zero_ether_addr(ev->cr.bssid))
  605. bssid = ev->cr.bssid;
  606. __cfg80211_connect_result(
  607. wdev->netdev, bssid,
  608. ev->cr.req_ie, ev->cr.req_ie_len,
  609. ev->cr.resp_ie, ev->cr.resp_ie_len,
  610. ev->cr.status,
  611. ev->cr.status == WLAN_STATUS_SUCCESS,
  612. NULL);
  613. break;
  614. case EVENT_ROAMED:
  615. __cfg80211_roamed(wdev, ev->rm.bssid,
  616. ev->rm.req_ie, ev->rm.req_ie_len,
  617. ev->rm.resp_ie, ev->rm.resp_ie_len);
  618. break;
  619. case EVENT_DISCONNECTED:
  620. __cfg80211_disconnected(wdev->netdev,
  621. ev->dc.ie, ev->dc.ie_len,
  622. ev->dc.reason, true);
  623. break;
  624. case EVENT_IBSS_JOINED:
  625. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
  626. break;
  627. }
  628. wdev_unlock(wdev);
  629. kfree(ev);
  630. spin_lock_irqsave(&wdev->event_lock, flags);
  631. }
  632. spin_unlock_irqrestore(&wdev->event_lock, flags);
  633. }
  634. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  635. {
  636. struct wireless_dev *wdev;
  637. ASSERT_RTNL();
  638. ASSERT_RDEV_LOCK(rdev);
  639. mutex_lock(&rdev->devlist_mtx);
  640. list_for_each_entry(wdev, &rdev->netdev_list, list)
  641. cfg80211_process_wdev_events(wdev);
  642. mutex_unlock(&rdev->devlist_mtx);
  643. }
  644. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  645. struct net_device *dev, enum nl80211_iftype ntype,
  646. u32 *flags, struct vif_params *params)
  647. {
  648. int err;
  649. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  650. ASSERT_RDEV_LOCK(rdev);
  651. /* don't support changing VLANs, you just re-create them */
  652. if (otype == NL80211_IFTYPE_AP_VLAN)
  653. return -EOPNOTSUPP;
  654. if (!rdev->ops->change_virtual_intf ||
  655. !(rdev->wiphy.interface_modes & (1 << ntype)))
  656. return -EOPNOTSUPP;
  657. /* if it's part of a bridge, reject changing type to station/ibss */
  658. if (dev->br_port && (ntype == NL80211_IFTYPE_ADHOC ||
  659. ntype == NL80211_IFTYPE_STATION))
  660. return -EBUSY;
  661. if (ntype != otype) {
  662. dev->ieee80211_ptr->use_4addr = false;
  663. switch (otype) {
  664. case NL80211_IFTYPE_ADHOC:
  665. cfg80211_leave_ibss(rdev, dev, false);
  666. break;
  667. case NL80211_IFTYPE_STATION:
  668. cfg80211_disconnect(rdev, dev,
  669. WLAN_REASON_DEAUTH_LEAVING, true);
  670. break;
  671. case NL80211_IFTYPE_MESH_POINT:
  672. /* mesh should be handled? */
  673. break;
  674. default:
  675. break;
  676. }
  677. cfg80211_process_rdev_events(rdev);
  678. }
  679. err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
  680. ntype, flags, params);
  681. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  682. if (!err && params && params->use_4addr != -1)
  683. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  684. if (!err) {
  685. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  686. switch (ntype) {
  687. case NL80211_IFTYPE_STATION:
  688. if (dev->ieee80211_ptr->use_4addr)
  689. break;
  690. /* fall through */
  691. case NL80211_IFTYPE_ADHOC:
  692. dev->priv_flags |= IFF_DONT_BRIDGE;
  693. break;
  694. case NL80211_IFTYPE_AP:
  695. case NL80211_IFTYPE_AP_VLAN:
  696. case NL80211_IFTYPE_WDS:
  697. case NL80211_IFTYPE_MESH_POINT:
  698. /* bridging OK */
  699. break;
  700. case NL80211_IFTYPE_MONITOR:
  701. /* monitor can't bridge anyway */
  702. break;
  703. case NL80211_IFTYPE_UNSPECIFIED:
  704. case __NL80211_IFTYPE_AFTER_LAST:
  705. /* not happening */
  706. break;
  707. }
  708. }
  709. return err;
  710. }
  711. u16 cfg80211_calculate_bitrate(struct rate_info *rate)
  712. {
  713. int modulation, streams, bitrate;
  714. if (!(rate->flags & RATE_INFO_FLAGS_MCS))
  715. return rate->legacy;
  716. /* the formula below does only work for MCS values smaller than 32 */
  717. if (rate->mcs >= 32)
  718. return 0;
  719. modulation = rate->mcs & 7;
  720. streams = (rate->mcs >> 3) + 1;
  721. bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
  722. 13500000 : 6500000;
  723. if (modulation < 4)
  724. bitrate *= (modulation + 1);
  725. else if (modulation == 4)
  726. bitrate *= (modulation + 2);
  727. else
  728. bitrate *= (modulation + 3);
  729. bitrate *= streams;
  730. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  731. bitrate = (bitrate / 9) * 10;
  732. /* do NOT round down here */
  733. return (bitrate + 50000) / 100000;
  734. }