util.c 23 KB

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