util.c 22 KB

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