util.c 26 KB

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