util.c 22 KB

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